Your Cart
Loading
Python code in Google Colab showing how to split a PDF into separate files based on page ranges using PyMuPDF.

How to Split a PDF into Page Ranges in Google Colab

If you have a multi-page PDF and want to turn specific page ranges into separate PDF files, you can do this easily with PyMuPDF in Google Colab.


For example, you can split a PDF like this:

  • Pages 1–3 → pages_1-3.pdf
  • Pages 4–6 → pages_4-6.pdf
  • Pages 7–10 → pages_7-10.pdf


1. Install PyMuPDF

First, install the PyMuPDF library:

How to Install PyMuPDF in Google Colab Using Python


2. Import Libraries

Then import the libraries you’ll need:

How to Import Libraries in Google Colab Using Python


3. Upload Your PDF

The following code opens a file picker so you can upload your PDF:

How to Upload Files in Google Colab Using Python



The uploaded file is stored in the variable input_pdf.


4. Create an Output Folder

Next, create an output folder called split_ranges where the individual PDF files will be saved:

How to Create an Output Folder in Google Colab Using Python


We set exist_ok=True so that Python won't return an error if the folder already exists.


5. Open the PDF

Next, open the uploaded PDF with PyMuPDF:

How to Open a PDF File with PyMuPDF


The variable doc now represents the entire PDF document.


6. Define the Page Ranges

Now define the page ranges you want to split from the PDF.

For example, if you want to create PDFs containing pages 1–3, 4–6, and 7–10:

How to Define Page Ranges With Python


The numbers represent the PDF page numbers, starting at 1.


7. Split the PDF into Page Ranges

The following loop goes through every page range. For each range, it creates a new PDF, copies the selected pages into it, saves it, and then closes the new PDF:

How to Split a PDF into Page Ranges Using Python


Here's what happens:

  • start is the first page of the range.
  • end is the last page of the range.
  • start - 1 converts the page number into a zero-based page index.
  • end - 1 converts the last page number into a zero-based page index.
  • A new empty PDF is created for each range.
  • insert_pdf() copies the selected pages into the new PDF.
  • save() creates a file such as pages_1-3.pdf.
  • close() closes the newly created PDF after it has been saved.


For example, a 10-page PDF with the ranges above will produce:


split_ranges/


├── pages_1-3.pdf

├── pages_4-6.pdf

└── pages_7-10.pdf


The first PDF contains pages 1, 2, and 3.

The second PDF contains pages 4, 5, and 6.

The third PDF contains pages 7, 8, 9, and 10.


8. Close the Original PDF

Once all the page ranges have been processed, close the original PDF:

How to Close a PDF Document Using Python


9. Create a ZIP File

Instead of downloading every PDF individually, you can put all the individual PDFs into a ZIP file:

How to Create a ZIP File Using Python


This creates:

split_ranges.zip

and automatically starts the download in Google Colab.


Complete Example

Splitting a PDF into Page Ranges in Google Colab



Open the notebook

Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.

[Open in Google Colab]



Ready to put this into practice? Check out the Python and PDF resources in our store.


👉 Visit the store



More PDF Python Resources

Want to learn how to Split a PDF into Separate PDF Pages in Google Colab?


Check out this mini-guide: How to Split a PDF into Separate PDF Pages in Google Colab.




Subscribe to our newsletter

Get notified when new PDF mini-guides, resources, templates, bundles, ebooks, and courses are released.