File size: 826 Bytes
5126a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 split_pdf(input_path, output_dir="output", max_pages=99):
    os.makedirs(output_dir, exist_ok=True)

    reader = PdfReader(input_path)
    total_pages = len(reader.pages)

    file_index = 0

    for start in range(0, total_pages, max_pages):
        writer = PdfWriter()
        end = min(start + max_pages, total_pages)

        for i in range(start, end):
            writer.add_page(reader.pages[i])

        output_path = os.path.join(output_dir, f"{file_index}.pdf")
        with open(output_path, "wb") as f:
            writer.write(f)

        print(f"Saved: {output_path} (pages {start}-{end-1})")
        file_index += 1


if __name__ == "__main__":
    input_pdf = "PhysRevD.110.030001.pdf"
    split_pdf(input_pdf, output_dir=input_pdf.replace('.pdf', ''))