Demerging the pdf file by Darpan-Balar · Pull Request #556 · Python-World/python-mini-projects · GitHub
Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file modified projects/.DS_Store
Binary file not shown.
Binary file added projects/Demerge_pdfs/.DS_Store
Binary file not shown.
Binary file added projects/Demerge_pdfs/Python.pdf
Binary file not shown.
Binary file added projects/Demerge_pdfs/Python1(270 pages).pdf
Binary file not shown.
Binary file added projects/Demerge_pdfs/Python2(270 pages): 270.pdf
Binary file not shown.
Binary file added projects/Demerge_pdfs/Python3(90 pages).pdf
Binary file not shown.
Binary file added projects/Demerge_pdfs/Python4(180 pages).pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions projects/Demerge_pdfs/README.md
40 changes: 40 additions & 0 deletions projects/Demerge_pdfs/demerging_pdfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import PyPDF2

# Note: here instead of Python.pdf you should give the whole path to the pdf if the pdf is not present in the same directory where python program is present
merged_pdf = open('Python.pdf', mode='rb')


pdf = PyPDF2.PdfFileReader(merged_pdf)

(u, ctr, x) = tuple([0]*3)
for i in range(1, pdf.numPages+1):

if u >= pdf.numPages:
print("Successfully done!")
exit(0)
name = input("Enter the name of the pdf: ")
ctr = int(input(f"Enter the number of pages for {name}: "))
u += ctr
if u > pdf.numPages:
print('Limit exceeded! ')
break

# Note: In the braces you should give the desired path of where new files should be stored
base_path = '{}.pdf'
# If you want to store the new pdfs in the same directory, then leave the braces empty
path = base_path.format(name)
f = open(path, mode='wb')
pdf_writer = PyPDF2.PdfFileWriter()

for j in range(x, x+ctr):
page = pdf.getPage(j)
pdf_writer.addPage(page)

x += ctr

pdf_writer.write(f)
f.close()


merged_pdf.close()
print("Successfully done!")
17 changes: 17 additions & 0 deletions projects/Demerge_pdfs/explanation.txt