Last active
October 23, 2021 11:44
-
-
Save gautamkrishnar/159dd510e652363420317991a2bacc2e to your computer and use it in GitHub Desktop.
Python code to split a large PDF file into muliple files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PyPDF2 | |
input_pdf = PyPDF2.PdfFileReader(open("document.pdf", "rb")) | |
num_of_pages_per_file = 10 | |
doc_counter = 1 | |
counter = 0 | |
output = PyPDF2.PdfFileWriter() | |
for split_range in [range(input_pdf.numPages)[x:x + num_of_pages_per_file] for x in | |
range(0, input_pdf.numPages, num_of_pages_per_file)]: | |
for _ in split_range: | |
output.addPage(input_pdf.getPage(counter)) | |
counter = counter + 1 | |
print("Saving page %s / %s " % (counter, input_pdf.numPages)) | |
with open("document-%s.pdf" % doc_counter, "wb") as outputStream: | |
output.write(outputStream) | |
doc_counter = doc_counter + 1 | |
output = PyPDF2.PdfFileWriter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PyPDF2==1.26.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment