Skip to content

Instantly share code, notes, and snippets.

@BrennanBarker
Last active April 9, 2021 20:29
Show Gist options
  • Select an option

  • Save BrennanBarker/cc1fdb833812dfef80b871c1b4719909 to your computer and use it in GitHub Desktop.

Select an option

Save BrennanBarker/cc1fdb833812dfef80b871c1b4719909 to your computer and use it in GitHub Desktop.
Export pages from a pdf
from PyPDF2 import PdfFileReader, PdfFileWriter
def extract_pages(in_fp, pages, out_fp):
"""Extract a list of pages from a pdf into a new pdf."""
pdf = PdfFileReader(input_fp)
pdf_writer = PdfFileWriter()
for p in pages:
pdf_writer.addPage(pdf.getPage(p - 1)) # PyPDF2 starts at 0...
with open(output_fp, '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