-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd1.py
More file actions
29 lines (23 loc) · 948 Bytes
/
d1.py
File metadata and controls
29 lines (23 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
from PyPDF2 import PdfReader, PdfWriter
def extract_pages(input_path, start_page, end_page, output_path="output.pdf"):
"""
Extracts pages from start_page to end_page (inclusive)
and saves them into a single PDF file.
Page numbers are 1-based.
"""
reader = PdfReader(input_path)
writer = PdfWriter()
# Validate page range
total_pages = len(reader.pages)
if start_page < 1 or end_page > total_pages or start_page > end_page:
raise ValueError(f"Invalid page range. PDF has {total_pages} pages.")
# Add selected pages
for i in range(start_page - 1, end_page): # 0-based indexing
writer.add_page(reader.pages[i])
# Write output
with open(output_path, "wb") as f:
writer.write(f)
print(f"Saved: {output_path} (Pages {start_page} to {end_page})")
if __name__ == "__main__":
extract_pages("input.pdf", start_page=9, end_page=17, output_path="17.pdf")