Last active
April 9, 2021 20:29
-
-
Save BrennanBarker/cc1fdb833812dfef80b871c1b4719909 to your computer and use it in GitHub Desktop.
Export pages from a pdf
This file contains hidden or 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
| 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