DEV Community

Cover image for How to Split a PDF into Separate PDF Pages in Google Colab
PDF Python Hub
PDF Python Hub

Posted on Edited on Originally published at payhip.com

How to Split a PDF into Separate PDF Pages in Google Colab

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

1. Install PyMuPDF

First, install the PyMuPDF library:

%pip install -q -U pymupdf
Enter fullscreen mode Exit fullscreen mode

2. Import Libraries

Then import the libraries you’ll need:

import pymupdf
from google.colab import files
from pathlib import Path
import shutil
Enter fullscreen mode Exit fullscreen mode

3. Upload Your PDF

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

uploaded = files.upload()

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

input_pdf = next(iter(uploaded))
Enter fullscreen mode Exit fullscreen mode

The uploaded file is stored in the variable input_pdf.

4. Create an Output Folder

Next, create an output folder called split_pages where the individual PDF pages will be saved:

output_dir = Path("split_pages")
output_dir.mkdir(exist_ok=True)
Enter fullscreen mode Exit fullscreen mode

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:

doc = pymupdf.open(input_pdf)
Enter fullscreen mode Exit fullscreen mode

The variable doc now represents the entire PDF document.

6. Split the PDF into Individual Pages

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

for i in range(len(doc)):
  new_pdf = pymupdf.open()
  new_pdf.insert_pdf(doc, from_page=i, to_page=i)
  new_pdf.save(output_dir / f"page_{i + 1}.pdf")
  new_pdf.close()
Enter fullscreen mode Exit fullscreen mode

Here's what happens:

  • range(len(doc)) generates the page indexes for every page in the PDF.
  • i is the page index, starting at 0.
  • A new empty PDF is created for each page.
  • insert_pdf() copies the current page into the new PDF.
  • save() creates a file such as page_1.pdf, page_2.pdf, and so on.
  • close() closes the newly created PDF after it has been saved.

For example, a 5-page PDF will produce:

split_pages/

├── page_1.pdf

├── page_2.pdf

├── page_3.pdf

├── page_4.pdf

└── page_5.pdf

7. Close the Original PDF

Once all the pages have been processed, close the original PDF:

doc.close()
Enter fullscreen mode Exit fullscreen mode

8. Create a ZIP File

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

shutil.make_archive("split_pages", "zip", "split_pages")
files.download("split_pages.zip")
Enter fullscreen mode Exit fullscreen mode

This creates:
split_pages.zip

and automatically starts the download in Google Colab.

Complete Example

%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_pages")
output_dir.mkdir(exist_ok=True)

doc = pymupdf.open(input_pdf)

for i in range(len(doc)):
  new_pdf = pymupdf.open()
  new_pdf.insert_pdf(doc, from_page=i, to_page=i)
  new_pdf.save(output_dir / f"page_{i + 1}.pdf")
  new_pdf.close()

doc.close()

shutil.make_archive("split_pages", "zip", "split_pages")
files.download("split_pages.zip")
Enter fullscreen mode Exit fullscreen mode

Why Split a PDF into Separate PDFs?

Splitting a PDF into individual pages is useful when you need to:

  • Extract specific pages from a larger document.
  • Process pages separately.
  • Upload individual pages as separate documents.
  • Share only certain pages with someone.
  • Organize a large PDF into smaller files.

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]


More PDF Python Resources

Want to learn how to count the number of pages in a PDF?

Check out this mini-guide: How to Count the Number of Pages in a PDF Using PyMuPDF

Top comments (0)