Python Program to Merge Multiple PDF Files

2025-11-27 python pdf merge merger pypdf2 pdf-tools utilities automation

A Python program that merges multiple PDF files into a single document using the PyPDF2 library. Users can specify any number of PDFs, and the tool combines them in the selected order, producing a clean, merged output PDF.

Explanation

A PDF Merger is a Python tool that allows you to combine multiple PDF files into one single PDF document. This is useful for joining chapters, merging invoices, combining reports, assembling study material, or organizing multiple downloaded documents into a single file.

This Python program uses the PyPDF2 library, specifically PdfMerger, to append multiple PDFs together in the exact order provided by the user. You can merge 2, 5, 10 or even 50 PDFs easily using this script.

Requirements

  • pip install PyPDF2

    Installs the PyPDF2 library used for merging PDF files.

  • Python version 3 or higher.
  • Two or more PDF files to merge.
  • User enters number of files + file paths.
  • Output file is saved with the name provided by user.

Code Explanation

from PyPDF2 import PdfMerger, PdfReader

Imports the classes required for reading and merging PDF files.

print(" PDF Merger ".center(40,"="))

Prints a title formatted neatly using = characters.

count = int(input("How many PDF files do you want to merge? ").strip())

Asks the user how many PDF files they want to combine.

pdf_files = []
for i in range(count):
    path = input(f"Enter path for PDF {i+1}: ").strip()
    pdf_files.append(path)

Collects file paths for each PDF the user wants to merge.

output_pdf = input("Enter output merged PDF name: ").strip() or "merged.pdf"

Takes the name for the final merged PDF file.

merger = PdfMerger()

Creates a PdfMerger object to combine PDFs.

for pdf in pdf_files:
    reader = PdfReader(pdf)
    merger.append(reader)
    print(f"Added: {pdf}")

Loops through all provided PDF paths and appends them to the merger object.

with open(output_pdf, "wb") as out:
    merger.write(out)

Saves the merged PDF into the output file.

print(f"PDFs merged successfully! Saved as: {output_pdf}")

Displays a success message after merging is finished.

Key Points

  • Merges 2 or more PDF files into one PDF.
  • Maintains the order of merging exactly as provided by the user.
  • Uses PyPDF2's PdfMerger class.
  • No limit on the number of PDF files.
  • Works on any PDF size or length.

Full Python Program

from PyPDF2 import PdfMerger, PdfReader

print(" PDF Merger ".center(40,"="))

count = int(input("How many PDF files do you want to merge? ").strip())

pdf_files = []
for i in range(count):
    path = input(f"Enter path for PDF {i+1}: ").strip()
    pdf_files.append(path)

output_pdf = input("Enter output merged PDF name: ").strip() or "merged.pdf"

merger = PdfMerger()

for pdf in pdf_files:
    reader = PdfReader(pdf)
    merger.append(reader)
    print(f"Added: {pdf}")

with open(output_pdf, "wb") as out:
    merger.write(out)

print(f"PDFs merged successfully! Saved as: {output_pdf}")

Output :

> py pdfMerger.py

============== PDF Merger ==============

How many PDF files do you want to merge? 3
Enter path for PDF 1: file1.pdf
Enter path for PDF 2: file2.pdf
Enter path for PDF 3: file3.pdf
Added: file1.pdf
Added: file2.pdf
Added: file3.pdf

PDFs merged successfully! Saved as: merged.pdf
1