If you want to split a PDF into multiple PDFs by selecting specific pages for each one, you can use Python and PyMuPDF in Google Colab.
For example, you can turn a 20-page PDF into 3 separate PDFs, each containing 5 pages of your choice.
1. Install PyMuPDF
First, run this code in a Google Colab cell to install PyMuPDF:
%pip install -q -U pymupdf
2. Import libraries
Next, import the libraries needed for the workflow:
from google.colab import files
import pymupdf
import os
import shutil
These libraries are used for:
-
files— uploading and downloading files in Google Colab -
pymupdf— opening and creating PDF files -
os— creating and working with folders -
shutil— creating the ZIP archive at the end
3. Upload your PDF
Use Google Colab's file upload dialog to select the PDF you want to split:
uploaded = files.upload()
if not uploaded:
raise ValueError("No PDF was uploaded.")
input_pdf = next(iter(uploaded))
After running the cell, click Choose Files and select your PDF.
The uploaded filename is stored in the input_pdf variable.
4. Open your PDF
Now open the uploaded PDF using PyMuPDF:
doc = pymupdf.open(input_pdf)
The doc object represents your original PDF document.
5. Specify the pages for each PDF
Create a dictionary called selections to specify which pages should be included in each output PDF:
Page numbers start at 0, so 0 is the first page, 1 is the second page, and so on.
For a 20-page PDF split into 3 parts of 5 pages each:
selections = {
"part1.pdf": [0, 1, 3, 4, 5],
"part2.pdf": [6, 9, 10, 12, 13],
"part3.pdf": [14, 16, 17, 18, 19]
}
The pages don't have to be consecutive, and you can arrange them in whatever order you specify.
6. Create the separate PDFs
Next, create a folder for the output files:
output_dir = "split_multiple_pdfs"
os.makedirs(output_dir, exist_ok=True)
Now loop through the selections and create each PDF:
for filename, pages in selections.items():
output = pymupdf.open()
for page_number in pages:
output.insert_pdf(
doc,
from_page=page_number,
to_page=page_number
)
output.save(os.path.join(output_dir, filename))
output.close()
doc.close()
Here's what this code does:
- Goes through each filename and its list of selected pages.
- Creates a new, empty PDF.
- Copies each selected page from the original PDF.
- Saves the new PDF inside the output folder.
- Closes the new PDF.
- Finally, closes the original PDF.
The resulting folder will look like this:
split_multiple_pdfs/
├── part1.pdf
├── part2.pdf
└── part3.pdf
7. Download all PDFs as a ZIP file
Instead of downloading each PDF separately, create a ZIP file containing all the parts:
zip_path = shutil.make_archive(
"split_multiple_pdfs",
"zip",
output_dir
)
This creates:
split_multiple_pdfs.zip
The ZIP file contains all of the PDFs from the split_multiple_pdfs folder.
8. Download the ZIP file
Finally, use Google Colab's files.download() function:
files.download(zip_path)
Your browser will download:
split_multiple_pdfs.zip
After extracting the ZIP file, you'll find all of your newly created PDFs inside.
Complete Example
%pip install -q -U pymupdf
from google.colab import files
import pymupdf
import os
import shutil
uploaded = files.upload()
if not uploaded:
raise ValueError("No PDF was uploaded.")
input_pdf = next(iter(uploaded))
doc = pymupdf.open(input_pdf)
selections = {
"part1.pdf": [0, 1, 3, 4, 5],
"part2.pdf": [6, 9, 10, 12, 13],
"part3.pdf": [14, 16, 17, 18, 19]
}
output_dir = "split_multiple_pdfs"
os.makedirs(output_dir, exist_ok=True)
for filename, pages in selections.items():
output = pymupdf.open()
for page_number in pages:
output.insert_pdf(
doc,
from_page=page_number,
to_page=page_number
)
output.save(os.path.join(output_dir, filename))
output.close()
doc.close()
zip_path = shutil.make_archive(
"split_multiple_pdfs",
"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.
This mini-guide was originally published on PDF Python Hub, where you can find more in-depth Python resources for working with PDFs.
More PDF Python Resources
Want to learn how to Split a PDF by Custom Page Selections in Google Colab?
Check out this mini-guide: How to Split a PDF by Custom Page Selections in Google Colab
Top comments (0)