October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Split PDF Documents with Python

Use PyMuPDF to extract an inclusive page range, split a PDF into individual pages or chunks, or use pypdf’s stop-exclusive range selection.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To split a PDF with Python, copy the page or page range you want into a new document, then save it. PyMuPDF’s Document.insert_pdf() provides a direct way to do this; its page indexes start at 0. For another option, pypdf can append a selected range using a start-inclusive, stop-exclusive tuple.

Extract a page range with PyMuPDF

PyMuPDF’s Document.insert_pdf() copies pages from one PDF document into another. The example below extracts pages 3 through 7 using ordinary, 1-based page numbers, with both endpoints included.

As an Amazon Associate I earn from qualifying purchases.

from pathlib import Path
import pymupdf

source_path = Path("input.pdf")
output_path = Path("selected-pages.pdf")

# User-facing page numbers are 1-based and inclusive.
first_page = 3
last_page = 7

with pymupdf.open(source_path) as source:
    if first_page < 1 or last_page < first_page or last_page > source.page_count:
        raise ValueError("Page range is outside the document")

    output = pymupdf.open()
    output.insert_pdf(
        source,
        from_page=first_page - 1,
        to_page=last_page - 1,
    )
    output.save(output_path)
    output.close()

Change input.pdf, the output filename, and the two page numbers for your files. The range check rejects a start below page 1, an end before the start, or an end beyond the document. PyMuPDF indexes pages from 0, so subtract 1 from both user-facing page numbers. Its to_page endpoint is inclusive: the converted last-page index is copied too. See the PyMuPDF tutorial on merging and splitting PDFs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Split into one PDF per page

To create a separate output for every page, iterate over the source indexes, create an empty document for each one, and insert only that page. This example names the files using 1-based page numbers.

from pathlib import Path
import pymupdf

source_path = Path("input.pdf")
output_dir = Path("pages")
output_dir.mkdir(exist_ok=True)

with pymupdf.open(source_path) as source:
    for index in range(source.page_count):
        output = pymupdf.open()
        output.insert_pdf(source, from_page=index, to_page=index)
        output.save(output_dir / f"page-{index + 1}.pdf")
        output.close()

Split into fixed-size chunks

For chunks of a chosen number of pages, advance by that size and cap the last chunk at the source’s final page. The example creates chunks of five pages, except when fewer remain.

from pathlib import Path
import pymupdf

source_path = Path("input.pdf")
output_dir = Path("chunks")
chunk_size = 5
output_dir.mkdir(exist_ok=True)

with pymupdf.open(source_path) as source:
    for start in range(0, source.page_count, chunk_size):
        end = min(start + chunk_size, source.page_count)
        output = pymupdf.open()
        output.insert_pdf(source, from_page=start, to_page=end - 1)
        output.save(output_dir / f"chunk-{start // chunk_size + 1}.pdf")
        output.close()

Here, start and end are zero-based indexes, and end is exclusive in the loop’s arithmetic. The conversion to to_page=end - 1 is needed because PyMuPDF’s to_page endpoint is inclusive.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use pypdf if it fits your project

pypdf offers a different range convention through PdfWriter.append(). Its page tuple uses zero-based indexes with an exclusive stop value: (2, 7) selects indexes 2 through 6, or pages 3 through 7 in ordinary numbering.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from pypdf import PdfWriter

writer = PdfWriter()
writer.append("input.pdf", pages=(2, 7))
writer.write("selected-pages.pdf")

Consult the pypdf guide to merging PDFs and its PdfWriter.append() API reference for the documented selection and write methods. The two libraries use different endpoint conventions; do not transfer a range unchanged between the examples.

Check the result

  • Confirm the requested range is within the source document’s page count before splitting.
  • Open the generated PDF files and check that the expected first and last pages are present.
  • For one-file-per-page or chunk output, confirm the output directory contains the expected files and that the final chunk is not empty.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.