A small command-line tool for merging PDF files, with support for selecting specific pages from each source file.
- Merge any number of PDF files into one, in a chosen order.
- Select specific pages or ranges from each file (e.g.
1-3,7) instead of always merging entire documents. - Two ways to use it: an interactive prompt-driven mode, or a scriptable command-line mode for use in shell scripts and automation.
- Clear error messages for invalid page ranges, missing files, or malformed input.
Requires Python 3.9 or later.
pip install -e .This installs the package in editable mode along with its pypdf dependency,
and exposes both the pdfmerger console script and the python -m pdfmerger
module entry point described below.
Run the tool with no arguments from a directory containing PDF files:
python -m pdfmergerIt will list the PDF files it finds, let you pick which ones to merge and in
what order, prompt for a page range for each one (or all), and ask for an
output filename.
Pass files directly for a non-interactive merge, optionally with a page range after a colon:
python -m pdfmerger report.pdf:1-3 appendix.pdf -o final.pdfThis merges pages 1-3 of report.pdf followed by every page of
appendix.pdf, writing the result to final.pdf.
Options:
| Flag | Description |
|---|---|
-o, --output |
Output filename (default: combined.pdf) |
-d, --directory |
Directory to resolve input/output files against (default: current directory) |
The tool is organized as a small set of single-responsibility classes rather
than one monolithic script, laid out under src/pdfmerger/:
PageRange(page_range.py) parses and validates page range strings such as1,3,5-7into concrete page indices.PDFDocument(document.py) represents a single PDF file on disk and exposes its page count.PDFFileScanner(scanner.py) discovers PDF files in a directory.PDFMergerService(merger.py) performs the actual merge given a list of documents and the pages to take from each.ConsoleUIandmain(cli.py) wire the above classes together into the interactive and command-line interfaces.
Separating parsing, file discovery, and merging from the user interface keeps each piece independently testable and easy to reason about.
pip install -r requirements.txt pytest
pytestThis project started as a first-year university exercise for learning Python. It has since been rewritten with a proper object-oriented design, a scriptable CLI, and test coverage.