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:
%pip install -q -U pymupdf
2. Import Libraries
Then import the libraries you’ll need:
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:
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:
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:
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:
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:
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:
-
startis the first page of the range. -
endis the last page of the range. -
start - 1converts the page number into a zero-based page index. -
end - 1converts 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:
doc.close()
9. Create a ZIP File
Instead of downloading every PDF individually, you can put all the individual PDFs into a ZIP file:
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
%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.
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.
Top comments (0)