Skip to content

Instantly share code, notes, and snippets.

@ivansaul
Created October 15, 2020 15:58
Show Gist options
  • Save ivansaul/8d434be2abc2bb37d89ce92add6a9b17 to your computer and use it in GitHub Desktop.
Save ivansaul/8d434be2abc2bb37d89ce92add6a9b17 to your computer and use it in GitHub Desktop.
Split PDF by range with Python v3.x and pypdf2
#!/usr/bin/python
from PyPDF2 import PdfFileReader, PdfFileWriter
#split range
pgi=30 #start
pgf=37 #end
pdf_document = "test.pdf"
pdf = PdfFileReader(pdf_document)
pdf_writer = PdfFileWriter()
for page in range(pgi-1,pgf):
current_page = pdf.getPage(page)
pdf_writer.addPage(current_page)
with open(f'test-{pgi}-{pgf}.pdf', "wb") as out:
pdf_writer.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment