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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To split a PDF with Python, create a new PDF and copy the pages you want into it. The PyMuPDF example below extracts a range using ordinary, 1-based page numbers; it also shows how to make one output file per page or split a document into fixed-size chunks.

Extract a page range with PyMuPDF

PyMuPDF’s Document.insert_pdf() copies pages from one PDF document into another. Create an empty destination document, insert the selected pages, then save the new file. The code uses user-facing page numbers, where the first page is page 1 and both ends of the requested range are included.

from pathlib import Path
import pymupdf

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

# Ordinary page numbers: both first_page and last_page are 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()

With the example values, the output contains pages 3 through 7 of the source. The conversion subtracts one from both page numbers because PyMuPDF’s page indexes are zero-based: index 0 is the first page. The tutorial’s to_page argument includes its endpoint; for example, to_page=9 selects through the tenth page. The range check catches requests below page 1, reversed ranges, and endpoints beyond the source document’s page_count. See the PyMuPDF tutorial for the documented page-copying method.

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.

Write one PDF per page or per chunk

For separate page files, open the source once and create a new destination for each zero-based page index. Use a filename that includes the ordinary page number to keep the outputs distinct:

from pathlib import Path
import pymupdf

source_path = Path("input.pdf")
output_dir = Path("split-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()

To split into chunks, set a chunk size and advance through the source in increments of that size. Clamp each chunk’s last page to the document’s final index so the final output can contain fewer pages than the others.

chunk_size = 10

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

Here, start and stop are zero-based loop boundaries, with stop excluded. The PyMuPDF call’s to_page is inclusive, so the code passes stop - 1. The filenames use 1-based page numbers, and the stated end page 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 another documented approach: append a selected page range to a PdfWriter, then write the output. Its (start, stop) tuple uses zero-based indexes and excludes stop.

Free tools Windows power users keep installed

One-click scans. No signup required.

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))  # indexes 2 through 6
writer.write("selected-pages.pdf")

This selects the third through seventh pages in ordinary page numbering. That endpoint convention differs from PyMuPDF’s inclusive to_page; when choosing between them, follow the API for the library already used in your project rather than assuming the range syntax is interchangeable. See pypdf’s page-merging documentation and PdfWriter API reference.

Check the output

  • Confirm that the requested start and end pages fall within the source document’s page count.
  • Open the generated PDF files and verify that each contains the intended pages.
  • For chunk output, check the last file separately; its page count may be smaller than the chosen chunk size.

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.