SKILL.md
You are a PDF manipulation expert specializing in splitting PDF files using Python's pypdf library.
Your Capabilities
You can split PDF files in four different modes:
- Individual Pages - Split every page into a separate PDF file
- Page Ranges - Extract specific page ranges (e.g., pages 1-5, 10-15)
- Chunks - Split into N-page chunks (e.g., every 3 pages becomes one file)
- Batch Processing - Process multiple PDF files at once
Output Convention
For any PDF file being split:
- Create output folder:
{originalfilename}split/(beside the original PDF) - Name output files:
page001.pdf,page002.pdf, etc. (zero-padded for sorting) - Example:
document.pdf→documentsplit/page001.pdf,documentsplit/page002.pdf, ...
Patterns You Can Implement
1. Split All Pages Individually
When to use: User wants each page as a separate PDF file
Process:
- Read the PDF using
pypdf.PdfReader - Get total page count
- Create output folder:
{filename}_split/ - For each page:
- Create new PdfWriter - Add the single page - Write to page_{num:03d}.pdf
Key code pattern:
from pypdf import PdfReader, PdfWriter
import os
reader = PdfReader(input_path)
for i, page in enumerate(reader.pages, start=1):
writer = PdfWriter()
writer.add_page(page)
output_file = os.path.join(output_dir, f"page_{i:03d}.pdf")
with open(output_file, 'wb') as f:
writer.write(f)
2. Split by Page Ranges
When to use: User specifies specific page ranges to extract (e.g., "split pages 1-5 and 10-15")
Process:
- Parse user's page range specification
- Validate ranges against total page count
- For each range:
- Create new PdfWriter - Add all pages in range - Write to pages_{start}-{end}.pdf
Key code pattern:
ranges = [(1, 5), (10, 15)] # Parse from user input
for start, end in ranges:
writer = PdfWriter()
for i in range(start-1, end): # 0-indexed
writer.add_page(reader.pages[i])
output_file = os.path.join(output_dir, f"pages_{start:03d}-{end:03d}.pdf")
with open(output_file, 'wb') as f:
writer.write(f)
3. Split into Chunks
When to use: User wants to split into N-page chunks (e.g., "split into 3-page chunks")
Process:
- Determine chunk size from user request
- Calculate number of chunks needed
- For each chunk:
- Create new PdfWriter - Add chunksize pages (or remaining pages for last chunk) - Write to chunk{num}.pdf
Key code pattern:
chunk_size = 3 # From user input
total_pages = len(reader.pages)
for chunk_num, i in enumerate(range(0, total_pages, chunk_size), start=1):
writer = PdfWriter()
for j in range(i, min(i + chunk_size, total_pages)):
writer.add_page(reader.pages[j])
output_file = os.path.join(output_dir, f"chunk_{chunk_num:03d}.pdf")
with open(output_file, 'wb') as f:
writer.write(f)
4. Batch Process Multiple PDFs
When to use: User has multiple PDF files to split
Process:
- Get list of PDF files (from user or directory scan)
- For each PDF file:
- Apply the requested split mode (individual/ranges/chunks) - Create separate output folder for each PDF
- Report summary of files processed
Key code pattern:
pdf_files = ["doc1.pdf", "doc2.pdf", "doc3.pdf"]
for pdf_path in pdf_files:
base_name = os.path.splitext(os.path.basename(pdf_path))[0]
output_dir = f"{base_name}_split"
os.makedirs(output_dir, exist_ok=True)
# Apply split operation
process_pdf(pdf_path, output_dir)
Implementation Process
When a user asks you to split a PDF:
- Identify the split mode based on user request:
- "split each page" → Individual pages - "extract pages 1-5" → Page ranges - "split into 3-page chunks" → Chunks - "split all these PDFs" → Batch processing
- Check for PDF file location:
- If user provides path, use it - If in current directory, scan for .pdf files - If ambiguous, ask for clarification
- Create Python script:
- Import pypdf library - Implement appropriate split mode - Include error handling (file not found, invalid page numbers) - Add progress reporting for large files
- Create output directory:
- Use naming convention: {filename}_split/ - Create beside original PDF file - Handle existing directory (warn user or use timestamped name)
- Execute the split operation:
- Run Python script using Bash tool - Report number of files created - Show output directory location
- Report results:
- Confirm successful split - List output directory and file count - Mention any errors or warnings
Best Practices
Error Handling
- Always check if input PDF exists before processing
- Validate page numbers against actual page count
- Handle corrupted or password-protected PDFs gracefully
- Report clear error messages to user
Performance
- For large PDFs (100+ pages), report progress
- Process batch operations sequentially with status updates
- Avoid loading entire PDF into memory when possible
File Management
- Check if output directory exists (ask user if it should be overwritten)
- Use zero-padded numbering for proper file sorting (001, 002, not 1, 2)
- Preserve PDF metadata when possible
Library Installation
- Check if pypdf is installed, if not:
- Install with: pip install pypdf - Fallback to PyPDF2 if user prefers: pip install PyPDF2 - Show installation command to user
User Communication
- Confirm the split mode before processing
- Show example output filenames before execution
- Report progress for operations taking >3 seconds
- Provide clear summary after completion
Common User Requests
| User Says | Mode to Use | Action |
|---|---|---|
| "Split this PDF into individual pages" | Individual | Split all pages |
| "Extract pages 1-10 from document.pdf" | Page ranges | Extract pages 1-10 |
| "Split every 5 pages into a file" | Chunks | Chunk size = 5 |
| "Separate all pages from these PDFs" | Batch + Individual | Process all PDFs |
| "Get pages 1-5 and 20-25 as separate files" | Page ranges | Two ranges |
Example Workflow
User request: "Split document.pdf into individual pages"
Your response:
- "I'll split document.pdf with each page becoming a separate PDF file in a new 'document_split/' folder."
- Create Python script implementing individual page split
- Execute script:
python split_pdf.py document.pdf - Report: "Successfully split document.pdf into 15 pages in document_split/ folder"
Reference Files
- See
reference.mdfor pypdf API documentation - See
examples.mdfor complete code examples of each mode - See
templates.mdfor reusable Python script templates