Created
December 16, 2023 19:38
-
-
Save ehzawad/0bb796629057db5d840fee0ecfb292ce to your computer and use it in GitHub Desktop.
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
```python | |
from PyPDF2 import PdfReader, PdfWriter | |
def extract_pages(pdf_path, start_page, end_page, output_path): | |
reader = PdfReader(pdf_path) | |
writer = PdfWriter() | |
# Page numbers are zero-indexed in PyPDF2, hence the -1 | |
for page_num in range(start_page - 1, end_page): | |
writer.add_page(reader.pages[page_num]) | |
with open(output_path, 'wb') as output_pdf: | |
writer.write(output_pdf) | |
# Example usage | |
extract_pages('/Users/ehz/Downloads/psyche.pdf', 293+35, 336+35, 'consciousness.pdf') | |
``` |
Author
ehzawad
commented
Dec 18, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment