-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_pdf_pages.py
50 lines (41 loc) · 1.64 KB
/
extract_pdf_pages.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import argparse
from PyPDF2 import PdfReader, PdfWriter
def extract_pages(input_pdf, output_pdf, page_numbers):
"""
Extracts specific pages from a PDF and saves them as a new PDF.
Args:
input_pdf (str): Path to the input PDF file.
output_pdf (str): Path to save the extracted pages PDF.
page_numbers (list): List of page numbers to extract (0-based index).
"""
reader = PdfReader(input_pdf)
writer = PdfWriter()
for page_num in page_numbers:
if page_num < len(reader.pages):
writer.add_page(reader.pages[page_num])
else:
print(f"Warning: Page number {page_num} is out of range.")
with open(output_pdf, "wb") as output_file:
writer.write(output_file)
print(f"Extracted pages saved to {output_pdf}")
def main():
# Set up argument parser
parser = argparse.ArgumentParser(description="Extract specific pages from a PDF.")
parser.add_argument("input_pdf", help="Path to the input PDF file")
parser.add_argument("output_pdf", help="Path to save the extracted pages PDF")
parser.add_argument(
"pages",
help="Comma-separated list of page numbers to extract (1-based index)",
type=str
)
args = parser.parse_args()
# Convert 1-based page numbers to 0-based and handle input
try:
pages_to_extract = [int(p) - 1 for p in args.pages.split(",")]
except ValueError:
print("Error: Pages must be a comma-separated list of integers.")
return
# Extract the specified pages
extract_pages(args.input_pdf, args.output_pdf, pages_to_extract)
if __name__ == "__main__":
main()