Certainly! If your PDF files are large and uploading them to Colab is inconvenient, running the script locally on your machine via command line (cmd) is a better idea.
Here is a complete simple Python script you can run from the command line to split your PDF by page ranges (e.g., pages 13-23 or just page 32):
Steps to run this locally (on Windows/macOS/Linux):
1. Install PyPDF2 if you don’t have it yet:
Open your terminal or cmd and run:
pip install PyPDF2
2. Create a Python script
Copy the following code into a file named, for example, split_pdf.py:
import sys
from PyPDF2 import PdfReader, PdfWriter
def split_pdf(input_pdf_path, start_page, end_page=None, output_pdf_path="output.pdf"):
reader = PdfReader(input_pdf_path)
writer = PdfWriter()
num_pages = len(reader.pages)
if end_page is None:
end_page = start_page
if not (1 <= start_page <= num_pages) or not (1 <= end_page <= num_pages):
raise ValueError(f"Page numbers out of range. Document has {num_pages} pages.")
if start_page > end_page:
raise ValueError("start_page cannot be greater than end_page.")
for page_num in range(start_page-1, end_page):
writer.add_page(reader.pages[page_num])
with open(output_pdf_path, "wb") as out_pdf:
writer.write(out_pdf)
print(f"Created PDF: {output_pdf_path} with pages {start_page} to {end_page}")
def print_usage():
print("Usage: python split_pdf.py <input_pdf> <start_page> [<end_page>] [<output_pdf>]")
print("Example 1: python split_pdf.py myfile.pdf 13 23 pages_13_to_23.pdf")
print("Example 2: python split_pdf.py myfile.pdf 32 page_32.pdf")
if __name__ == "__main__":
args = sys.argv[1:]
if len(args) < 2:
print_usage()
sys.exit(1)
input_pdf = args[0]
try:
start_page = int(args[1])
except ValueError:
print("Start page must be an integer.")
sys.exit(1)
# Default values
end_page = None
output_pdf = "output.pdf"
if len(args) == 3:
# Could be just output filename or end_page
try:
# try to parse as end_page
end_page = int(args[2])
except ValueError:
# if not int, that's output filename, extract just one page
output_pdf = args[2]
elif len(args) >= 4:
try:
end_page = int(args[2])
except ValueError:
print("End page must be an integer.")
sys.exit(1)
output_pdf = args[3]
split_pdf(input_pdf, start_page, end_page, output_pdf)
3. Run the script from the command line
Open your terminal or cmd in the folder where the script and your PDF file are located, then run commands like:
python split_pdf.py my_big_pdf.pdf 13 23 pages_13_to_23.pdf
This extracts pages 13 through 23 into pages_13_to_23.pdf.
Or just a single page, e.g., page 32:
python split_pdf.py my_big_pdf.pdf 32 page_32.pdf
This extracts page 32 into page_32.pdf.
Summary:
- Install PyPDF2 (
pip install PyPDF2)
- Save the above Python script (
split_pdf.py)
- Run from cmd/terminal like:
python split_pdf.py input.pdf 13 23 output.pdf
or
python split_pdf.py input.pdf 32 output.pdf
If you want, I can also help you make it even friendlier (e.g., with GUI or better error handling). Just ask!