Combine multiple PDFs into one or split a PDF into separate files. Use when a user asks to merge PDFs, combine PDF files, join documents together, split a PDF into pages, extract pages from a PDF, or separate a PDF into parts. Supports page range selection and custom ordering.
Combine multiple PDF files into a single document or split a PDF into separate files by page ranges. This skill handles merging in a specified order, splitting by page numbers, extracting specific pages, and preserving bookmarks and metadata where possible.
When a user asks to merge or split PDF files, follow these steps:
Ask or infer what the user needs:
Check that all input files exist and are valid PDFs:
import os
from PyPDF2 import PdfReader
def validate_pdfs(file_paths):
results = []
for path in file_paths:
if not os.path.exists(path):
results.append({"file": path, "status": "not found"})
continue
try:
reader = PdfReader(path)
results.append({
"file": path,
"status": "valid",
"pages": len(reader.pages)
})
except Exception as e:
results.append({"file": path, "status": f"invalid: {e}"})
return results
For merging:
from PyPDF2 import PdfMerger
def merge_pdfs(input_paths, output_path):
merger = PdfMerger()
for path in input_paths:
merger.append(path)
merger.write(output_path)
merger.close()
return output_path
For splitting by page ranges:
from PyPDF2 import PdfReader, PdfWriter
def split_pdf(input_path, ranges, output_dir="."):
reader = PdfReader(input_path)
output_files = []
for i, (start, end) in enumerate(ranges):
writer = PdfWriter()
for page_num in range(start - 1, min(end, len(reader.pages))):
writer.add_page(reader.pages[page_num])
output_path = os.path.join(output_dir, f"split_{i+1}_pages_{start}-{end}.pdf")
with open(output_path, "wb") as f:
writer.write(f)
output_files.append(output_path)
return output_files
For extracting specific pages:
def extract_pages(input_path, page_numbers, output_path):
reader = PdfReader(input_path)
writer = PdfWriter()
for page_num in page_numbers:
if 1 <= page_num <= len(reader.pages):
writer.add_page(reader.pages[page_num - 1])
with open(output_path, "wb") as f:
writer.write(f)
return output_path
After the operation, verify the output:
User request: "Combine report-q1.pdf, report-q2.pdf, and report-q3.pdf into annual-report.pdf"
Actions taken:
Output:
Merged 3 PDF files into annual-report.pdf
Input files:
1. report-q1.pdf (12 pages)
2. report-q2.pdf (15 pages)
3. report-q3.pdf (11 pages)
Output: annual-report.pdf (38 pages, 2.4 MB)
User request: "Split textbook.pdf into separate files: pages 1-30, 31-55, 56-80"
Actions taken:
Output:
Split textbook.pdf into 3 files:
1. split_1_pages_1-30.pdf (30 pages, 1.1 MB)
2. split_2_pages_31-55.pdf (25 pages, 0.9 MB)
3. split_3_pages_56-80.pdf (25 pages, 0.8 MB)
All files saved to current directory.
User request: "Pull out pages 5, 12, and 18-22 from presentation.pdf"
Actions taken:
Output:
Extracted 7 pages from presentation.pdf
Pages extracted: 5, 12, 18, 19, 20, 21, 22
Output: extracted_pages.pdf (7 pages, 540 KB)
pip install PyPDF2 if not available. For advanced features like preserving form fields, use pikepdf instead.npx skills add TerminalSkills/pdf-merge-split下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Edit PDFs with natural-language instructions using the nano-pdf CLI.
Control Sonos speakers (discover/status/play/volume/group).
Terminal Spotify playback/search via spogo (preferred) or spotify_player.
Capture frames or clips from RTSP/ONVIF cameras.
CLI to manage emails via IMAP/SMTP. Use `himalaya` to list, read, write, reply, forward, search, and organize emails from the terminal. Supports multiple accounts and message composition with MML (MIME Meta Language).
Monitor blogs and RSS/Atom feeds for updates using the blogwatcher CLI.
Category:tools