# 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:

```python
%pip install -q -U pymupdf
```

## 2\. Import Libraries

Then import the libraries you’ll need:

```python
import pymupdf
from google.colab import files
from pathlib import Path
import shutil
```

## 3\. Upload Your PDF

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

```python
uploaded = files.upload()

if not uploaded:
  raise ValueError("No PDF was uploaded.")

input_pdf = next(iter(uploaded))
```

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:

```python
output_dir = Path("split_ranges")
output_dir.mkdir(exist_ok=True)
```

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:

```python
doc = pymupdf.open(input_pdf)
```

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:

```python
ranges = [
    (1, 3),
    (4, 6),
    (7, 10)
]
```

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:

```python
for start, end in ranges:

    new_pdf = pymupdf.open()

    new_pdf.insert_pdf(
        doc,
        from_page=start - 1,
        to_page=end - 1
    )

    output_path = output_dir / f"pages_{start}-{end}.pdf"

    new_pdf.save(output_path)
    new_pdf.close()
```

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:

```python
doc.close()
```

## 9\. Create a ZIP File

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

```python
zip_path = shutil.make_archive(
    "split_ranges",
    "zip",
    output_dir)

files.download(zip_path)
```

This creates:

**split\_ranges.zip**

and automatically starts the download in Google Colab.

## Complete Example

```python
%pip install -q -U pymupdf

import pymupdf
from google.colab import files
from pathlib import Path
import shutil

uploaded = files.upload()

if not uploaded:
  raise ValueError("No PDF was uploaded.")

input_pdf = next(iter(uploaded))

output_dir = Path("split_ranges")
output_dir.mkdir(exist_ok=True)

doc = pymupdf.open(input_pdf)

ranges = [
    (1, 3),
    (4, 6),
    (7, 10)
]

for start, end in ranges:

    new_pdf = pymupdf.open()

    new_pdf.insert_pdf(
        doc,
        from_page=start - 1,
        to_page=end - 1
    )

    output_path = output_dir / f"pages_{start}-{end}.pdf"

    new_pdf.save(output_path)
    new_pdf.close()

doc.close()

zip_path = shutil.make_archive(
    "split_ranges",
    "zip",
    output_dir)

files.download(zip_path)
```

* * *

## 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](https://colab.research.google.com/drive/18O9KIIFxT7sX6_ikwB8e35iYYg2Ms-kL?usp=sharing)\]

* * *

## 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.](https://payhip.com/PDFPythonHub/blog/news/how-to-split-a-pdf-into-separate-pdf-pages-in-google-colab)
