PDF Page Splitter Using Python

2025-11-27 python pdf split splitter pypdf2 automation pdf-tools utilities

A Python-based PDF page splitter tool that divides a PDF into two separate files at a user-defined page number. Built using PyPDF2, it efficiently extracts pages into two new PDFs, making it ideal for splitting large documents, books, and reports.

Explanation

A PDF Page Splitter is a Python utility program that divides a single PDF file into two separate PDF documents at a page number chosen by the user. This is useful when you want to break large PDFs into smaller sections, split chapters from eBooks, or separate different parts of a report or document.

The program uses Python’s PyPDF2 library to read the input PDF, validate the split page number, and create two new PDF files. These output files contain:
✔ Pages from the beginning to the split page
✔ Pages from the split page to the end

Requirements

  • pip install PyPDF2

    Installs the PyPDF2 library required for splitting PDF files.

  • Python version 3 or higher.
  • A valid PDF file that you want to split.
  • User must choose the exact page number to split at.
  • Generates two output PDF files automatically.

Code Explanation

from PyPDF2 import PdfReader, PdfWriter

Imports the required classes for reading and writing PDF files.

print("=== PDF Splitter (Split at User Page Number) ===")

Prints a header so the user knows the program’s purpose.

input_pdf = input("Enter input PDF path: ").strip()

Asks the user for the PDF file to be split.

split_page = int(input("Enter the page number to split at: ").strip())

Gets the page number where the PDF should be split.

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

Reads the PDF and counts how many pages it contains.

if split_page < 1 or split_page >= total_pages:
print("Invalid page number. Cannot split.")
exit()

Validates the page number to ensure splitting is possible.

output_pdf_1 = input("Enter name for first file: ").strip() or "part1.pdf"
output_pdf_2 = input("Enter name for second file: ").strip() or "part2.pdf"

User names the two output PDF files.

writer1 = PdfWriter()
for i in range(0, split_page):
    writer1.add_page(reader.pages[i])

Creates the first PDF containing pages from 1 to the split page.

with open(output_pdf_1, "wb") as f1:
    writer1.write(f1)

Writes the first output PDF file.

writer2 = PdfWriter()
for i in range(split_page, total_pages):
    writer2.add_page(reader.pages[i])

Creates the second PDF containing the remaining pages.

with open(output_pdf_2, "wb") as f2:
    writer2.write(f2)

Writes the second output PDF file.

print("Split complete!")

Displays success message after splitting the PDF.

Key Points

  • Splits PDF into two smaller PDF files.
  • User chooses the page number where the split occurs.
  • Uses PyPDF2 for efficient PDF handling.
  • Ensures the split is valid with error checking.
  • Works for PDFs of any size.

Full Python Program

from PyPDF2 import PdfReader, PdfWriter

print("=== PDF Splitter (Split at User Page Number) ===")

input_pdf = input("Enter input PDF path: ").strip()
split_page = int(input("Enter the page number to split at: ").strip())

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

if split_page < 1 or split_page >= total_pages:
    print("Invalid page number. Cannot split.")
    exit()

output_pdf_1 = input("Enter name for first file: ").strip() or "part1.pdf"
output_pdf_2 = input("Enter name for second file: ").strip() or "part2.pdf"

writer1 = PdfWriter()
for i in range(0, split_page):
    writer1.add_page(reader.pages[i])

with open(output_pdf_1, "wb") as f1:
    writer1.write(f1)

writer2 = PdfWriter()
for i in range(split_page, total_pages):
    writer2.add_page(reader.pages[i])

with open(output_pdf_2, "wb") as f2:
    writer2.write(f2)

print("\nSplit complete!")
print(f"->{output_pdf_1}: pages 1 → {split_page}")
print(f"->{output_pdf_2}: pages {split_page + 1} → {total_pages}")

Output :

> py pdfSplitter.py

=== PDF Splitter (Split at User Page Number) ===
Enter input PDF path: book.pdf
Enter the page number to split at: 5
Enter name for first file: partA.pdf
Enter name for second file: partB.pdf

Split complete!
->partA.pdf: pages 1 → 5
->partB.pdf: pages 6 → 120
1