Skip to content

Instantly share code, notes, and snippets.

@gautamkrishnar
Last active October 23, 2021 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautamkrishnar/159dd510e652363420317991a2bacc2e to your computer and use it in GitHub Desktop.
Save gautamkrishnar/159dd510e652363420317991a2bacc2e to your computer and use it in GitHub Desktop.
Python code to split a large PDF file into muliple files
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment