PdfFileMerger in Python: Combine PDF Files

I’ve often found myself needing to merge multiple PDF files into one. Python has several libraries for working with PDFs, but one of the simplest and most effective tools I use is PdfFileMerger from the PyPDF2 package.

In this article, I’ll walk you through how to use PdfFileMerger in Python. I’ll share practical examples, including the full code, so you can start merging PDFs in your projects right away.

Let’s get in!

What is PdfFileMerger?

PdfFileMerger is a class within the PyPDF2 library designed specifically to merge multiple PDF files into a single PDF file. It handles everything from appending files, inserting pages, and even merging PDFs from URLs or streams.

The best part? You don’t need to install heavy or complicated software. Just Python and the PyPDF2 library, and you’re good to go.

How to Install PyPDF2

Before we dive into the code, you need to install the PyPDF2 package if you haven’t already.

Open your terminal or command prompt and run:

pip install PyPDF2

This will install the latest version of PyPDF2, which includes the PdfFileMerger class.

Check out Find the Area of a Square in Python

Method 1: Merge PDFs Using PdfFileMerger (Basic)

This is the simple way to merge PDF files. Suppose you have three PDF files from different departments of a company in New York, and you want to combine them into a single report.

Here’s the full Python code to do this:

from PyPDF2 import PdfFileMerger

def merge_pdfs(pdf_list, output_path):
    merger = PdfFileMerger()

    for pdf in pdf_list:
        merger.append(pdf)

    merger.write(output_path)
    merger.close()
    print(f"Merged PDF saved as {output_path}")

if __name__ == "__main__":
    # List of PDF files to merge
    pdf_files = ['finance_report.pdf', 'sales_report.pdf', 'hr_report.pdf']

    # Output file path
    output_file = 'combined_company_report.pdf'

    merge_pdfs(pdf_files, output_file)

I executed the above example code and added the screenshot below.

PdfFileMerger in Python

How this works:

  • We create an instance of PdfFileMerger.
  • Loop through the list of PDF files and append each one.
  • Write the merged PDF to the output file.
  • Close the merger to free resources.

This method works well when you want to merge entire PDF files in the order they appear.

Method 2: Merge PDFs with Page Ranges

Sometimes, you don’t need the entire PDF file, maybe just specific pages. For example, you want to combine only the first 3 pages of the finance report and all pages from the sales report.

Here’s how you can do that:

from PyPDF2 import PdfFileMerger

def merge_pdfs_with_page_ranges(pdf_page_ranges, output_path):
    merger = PdfFileMerger()

    for pdf, pages in pdf_page_ranges.items():
        if pages:
            for page in pages:
                merger.append(pdf, pages=(page, page+1))
        else:
            merger.append(pdf)

    merger.write(output_path)
    merger.close()
    print(f"Merged PDF saved as: {output_path}")

if __name__ == "__main__":
    # Dictionary with PDF files as keys and list of pages to include as values
    # Page numbers are zero-indexed
    pdf_pages = {
        'finance_report.pdf': [0, 1, 2],  # First 3 pages
        'sales_report.pdf': None,          # All pages
        'hr_report.pdf': [0, 4]            # First and fifth page only
    }

    output_file = 'custom_combined_report.pdf'

    merge_pdfs_with_page_ranges(pdf_pages, output_file)

I executed the above example code and added the screenshot below.

PdfFileMerger in Python Combine PDF Files

Explanation:

  • We pass a dictionary where keys are PDF filenames and values are lists of pages to include.
  • If the value is None, all pages are appended.
  • The pages parameter in append takes a tuple (start, end), exclusive note.
  • This method gives you the flexibility to merge only the parts you need.

Read Save Images in Python

Method 3: Insert PDFs at Specific Positions

What if you want to insert a PDF file in between the pages of another PDF? For example, adding a cover page before the main report.

Here’s how:

from PyPDF2 import PdfFileMerger

def merge_with_insertion(main_pdf, insert_pdf, output_path, insert_position=0):
    merger = PdfFileMerger()

    # Append pages before the insertion point
    merger.append(main_pdf, pages=(0, insert_position))

    # Append the insert PDF
    merger.append(insert_pdf)

    # Append the remaining pages from the main PDF
    merger.append(main_pdf, pages=(insert_position, None))

    merger.write(output_path)
    merger.close()
    print(f"Merged PDF with insertion saved as {output_path}")

if __name__ == "__main__":
    main_report = 'combined_company_report.pdf'
    cover_page = 'cover_page.pdf'
    output_file = 'final_report_with_cover.pdf'

    # Insert cover page at the beginning (position 0)
    merge_with_insertion(main_report, cover_page, output_file, insert_position=0)

How this works:

  • We append pages from the main PDF before the insertion point.
  • Then append the insert PDF (cover page).
  • Finally, append the remaining pages from the main PDF.
  • This approach is useful for adding intros, disclaimers, or annexes.

Tips from My Experience

  • Always close the PdfFileMerger object after writing the output file to avoid file locks.
  • If you get errors about corrupted PDFs, try opening the PDFs manually to check their integrity.
  • PyPDF2 doesn’t support encrypted PDFs out of the box. You’ll need to decrypt them first.
  • For large PDF files, merging can be slow; consider optimizing PDFs before merging.
  • Keep your file paths absolute or ensure your script runs in the correct directory to avoid file-not-found errors.

Merging PDFs using PdfFileMerger in Python is a powerful way to automate document workflows. Whether you’re combining monthly reports for a Chicago-based company or merging contracts from different clients, this method makes your task effortless and repeatable.

Try out the methods above, and you’ll see how much time you save compared to manual merging. If you have any questions or want me to cover more PDF manipulations, feel free to ask!

Other Python articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.