input pdf, output chapter wise split with page numbers, finally zip all files in zip file

 !pip install PyPDF2








import os
import zipfile
from PyPDF2 import PdfReader, PdfWriter

# Input and output paths
input_pdf_path = "/content/input.pdf"
output_folder = "/content"
zip_file_path = os.path.join(output_folder, "fileziped.zip")

# Define page ranges (1-based inclusive ranges)
page_ranges = {
    "file1.pdf": (15, 25),
    "file2.pdf": (26, 84),
    "file3.pdf": (85, 130),
    "file4.pdf": (131, 142),
    "file5.pdf": (143, 155),
    "file6.pdf": (156, 157),
}

# Read input PDF
reader = PdfReader(input_pdf_path)

# Store generated files for zipping
generated_files = []

# Extract specified page ranges
for filename, (start, end) in page_ranges.items():
    writer = PdfWriter()
   
    # Adjusting for 0-based indexing
    for i in range(start - 1, end):
        if i < len(reader.pages):
            writer.add_page(reader.pages[i])
   
    output_path = os.path.join(output_folder, filename)
    with open(output_path, "wb") as f:
        writer.write(f)
    generated_files.append(output_path)

# Create a ZIP file containing all split PDFs
with zipfile.ZipFile(zip_file_path, "w") as zipf:
    for file in generated_files:
        zipf.write(file, os.path.basename(file))

print("✅ PDF split successfully and zipped at:", zip_file_path)

Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

SAP CPI camel conditions and xpath conditions

oauth call to cpi integraiton suite from sap apim