Merge Multiple PDF Files Automatically Using Python
Merging multiple PDF documents into a single file can be fully automated using Python's modern pypdf library (the actively maintained successor to PyPDF2). This guide covers batch-merging an entire folder, merging files in a custom sequence, and extracting specific page ranges.
Prerequisites and Installation
Install pypdf using pip. It has no external C-dependencies and runs natively across all major platforms.
pip install pypdf
Method 1: Merge All PDFs in a Folder Automatically
To batch-merge every PDF inside a target directory, use Python's built-in pathlib module alongside pypdf.PdfMerger. This script scans the folder, sorts files alphabetically, and exports a unified PDF.
from pathlib import Path
from pypdf import PdfMerger
def merge_folder_pdfs(input_folder: str, output_path: str) -> None:
target_dir = Path(input_folder)
merger = PdfMerger()
# Locate and sort all .pdf files in target_dir
pdf_files = sorted(target_dir.glob("*.pdf"))
if not pdf_files:
raise FileNotFoundError(f"No PDF files found in {input_folder}")
for pdf_path in pdf_files:
merger.append(str(pdf_path))
# Export merged file and clean up resources
merger.write(output_path)
merger.close()
# Example usage
merge_folder_pdfs("./input_pdfs", "combined_document.pdf")
If you need to sort files by modification date rather than filename, replace sorted(target_dir.glob("*.pdf")) with:
pdf_files = sorted(target_dir.glob("*.pdf"), key=lambda p: p.stat().st_mtime)
Method 2: Merge Specific PDFs in Custom Order
When combining specific files where document sequence matters (such as combining a title page, body content, and appendix), pass an explicit list of paths to the merger instance.
from pypdf import PdfMerger
def merge_pdf_list(file_paths: list[str], output_path: str) -> None:
merger = PdfMerger()
for pdf in file_paths:
merger.append(pdf)
merger.write(output_path)
merger.close()
# Example usage
files_in_order = [
"01_cover_page.pdf",
"02_executive_summary.pdf",
"03_financial_report.pdf"
]
merge_pdf_list(files_in_order, "final_compiled_report.pdf")
Method 3: Extract and Merge Specific Page Ranges
To pull partial sections from different PDFs, use the pages parameter in merger.append(). The argument accepts a zero-indexed tuple formatted as (start_page, end_page).
from pypdf import PdfMerger
merger = PdfMerger()
# Append pages 0 to 4 (page index 5 excluded) from first doc
merger.append("document_a.pdf", pages=(0, 5))
# Append pages 10 to 14 from second doc
merger.append("document_b.pdf", pages=(10, 15))
merger.write("selected_pages_merged.pdf")
merger.close()
Handling Edge Cases
- Encrypted PDFs: If a source PDF is password-protected, open it via
PdfReader, callreader.decrypt("password"), and append the decrypted reader object instead of the file path. - Legacy Outlines/Bookmarks: By default,
pypdfpreserves table-of-contents bookmarks. Passimport_outline=Falseintomerger.append()to strip existing outlines and decrease output file size. - Memory Footprint: For extremely large batches (hundreds of PDFs), close the merger instance using a
try...finallyblock or explicitmerger.close()to prevent open file handles from consuming system RAM.
Need this done fast? order this script on FreelanceHunt.
I take on freelance fixes and builds in this area.