Skip to main content
The split command extracts specified pages or page ranges from a PDF document and saves them as separate PDF files.

Syntax

python solution.py split <input> <page_ranges>

Parameters

input
string
required
Path to the input PDF file to split
page_ranges
string
required
Comma-separated page numbers and ranges (1-based, inclusive)Syntax:
  • Single page: 5
  • Page range: 1-3
  • Multiple ranges: 1-3,5,7-9
  • Mixed: Combine individual pages and ranges

Page Range Syntax

Page numbers are 1-based and inclusive:
SyntaxDescriptionPages Extracted
1Single pagePage 1
1-3RangePages 1, 2, 3
1,3,5Individual pagesPages 1, 3, 5
1-3,5Range + individualPages 1, 2, 3, 5
1-3,5,7-9MixedPages 1, 2, 3, 5, 7, 8, 9

Usage Examples

Extract Specific Pages

python solution.py split document.pdf "1-3,5,7-9"
Expected Output:
[[0, 1, 2], [4], [6, 7, 8]]
Success: Created document_part1.pdf with pages [0, 1, 2]
Success: Created document_part2.pdf with pages [4]
Success: Created document_part3.pdf with pages [6, 7, 8]
Files Created:
  • document_part1.pdf - Contains pages 1, 2, 3
  • document_part2.pdf - Contains page 5
  • document_part3.pdf - Contains pages 7, 8, 9

Extract First 5 Pages

python solution.py split book.pdf "1-5"
Expected Output:
[[0, 1, 2, 3, 4]]
Success: Created book_part1.pdf with pages [0, 1, 2, 3, 4]
Files Created:
  • book_part1.pdf - Contains pages 1-5

Extract Individual Pages

python solution.py split manual.pdf "1,3,5,10"
Expected Output:
[[0], [2], [4], [9]]
Success: Created manual_part1.pdf with pages [0]
Success: Created manual_part2.pdf with pages [2]
Success: Created manual_part3.pdf with pages [4]
Success: Created manual_part4.pdf with pages [9]
Files Created:
  • manual_part1.pdf - Page 1
  • manual_part2.pdf - Page 3
  • manual_part3.pdf - Page 5
  • manual_part4.pdf - Page 10

Extract Multiple Ranges

python solution.py split report.pdf "1-2,10-15,25"
Expected Output:
[[0, 1], [9, 10, 11, 12, 13, 14], [24]]
Success: Created report_part1.pdf with pages [0, 1]
Success: Created report_part2.pdf with pages [9, 10, 11, 12, 13, 14]
Success: Created report_part3.pdf with pages [24]
Files Created:
  • report_part1.pdf - Pages 1-2
  • report_part2.pdf - Pages 10-15
  • report_part3.pdf - Page 25

Output File Naming Convention

The split command creates files using this pattern:
{original_filename}_part{number}.pdf
Where:
  • {original_filename} is the input filename without extension
  • {number} is a sequential counter (1, 2, 3, …)
Examples:
Input FilePage RangesOutput Files
document.pdf1-3,5document_part1.pdf, document_part2.pdf
book.pdf1,5,10book_part1.pdf, book_part2.pdf, book_part3.pdf
report-2024.pdf1-5report-2024_part1.pdf

Real-World Examples

Extract Cover and Back Pages

python solution.py split brochure.pdf "1,12"
Extracts the first and last pages of a 12-page brochure.

Split by Sections

python solution.py split thesis.pdf "1-10,11-25,26-50"
Splits a thesis into three sections:
  • Introduction (pages 1-10)
  • Literature Review (pages 11-25)
  • Methodology (pages 26-50)

Extract Specific Chapters

python solution.py split textbook.pdf "45-67,120-145"
Extracts chapters 3 and 7 from a textbook.

Extract Every 10th Page

python solution.py split large-doc.pdf "10,20,30,40,50"
Samples pages at regular intervals for review.

How It Works

  1. Parse Ranges: Converts the page range string into Python lists
    • "1-3,5,7-9" becomes [[0,1,2], [4], [6,7,8]]
  2. Page Extraction: For each range/page set:
    • Creates a new PdfWriter object
    • Adds specified pages from the input PDF
    • Writes to a new file with sequential naming
  3. Validation: Checks that page numbers are within the PDF’s page count
  4. Output: Creates separate files for each comma-separated range/page

Notes

  • Page numbers are 1-based (page 1 is the first page)
  • Page ranges are inclusive (1-3 includes pages 1, 2, and 3)
  • Each comma-separated group creates a separate output file
  • Output files are saved in the current working directory
  • Invalid page numbers trigger a warning but don’t stop the process
  • The original PDF is never modified

Page Number Reference

Understanding 1-based vs 0-based indexing:
User Input (1-based)Internal Index (0-based)Meaning
10First page
21Second page
54Fifth page
1-30, 1, 2First three pages
The tool handles this conversion automatically - you always specify pages using normal page numbers (starting from 1).

Error Handling

Page Out of Range

python solution.py split document.pdf "1-100"
If the PDF only has 50 pages:
Warning: Page 51 is out of range.
Warning: Page 52 is out of range.
...
Success: Created document_part1.pdf with pages [0, 1, 2, ..., 49]

Invalid Page Range Syntax

python solution.py split document.pdf "1--3"  # Double dash
Error: Invalid page range syntax

File Not Found

python solution.py split missing.pdf "1-5"
Error: Input file missing.pdf does not exist or is not a file.

Common Use Cases

1. Extract Front Matter

python solution.py split book.pdf "1-10"
Extract preface, table of contents, and introduction.

2. Separate Contract Pages

python solution.py split contract.pdf "1,5,12"
Extract signature pages and key clauses.

3. Create Chapter Excerpts

python solution.py split textbook.pdf "34-67"
Extract a single chapter for distribution.

4. Split Large PDF by Sections

python solution.py split manual.pdf "1-50,51-100,101-150"
Break a large manual into manageable sections.
  • merge - Combine multiple PDFs into one
  • convert - Convert between different file formats

Build docs developers (and LLMs) love