python pdf merge

May 8, 2025 19 min read

Merging PDF files is a common necessity in various professional and personal scenarios. Whether it's combining reports, assembling chapters of a book, or organizing scanned documents, the ability to seamlessly merge PDFs is invaluable. The need arises from the desire to consolidate information into a single, easily manageable file, enhancing efficiency and organization.

Effortlessly Merge PDFs with BreezePDF!

Combine your PDF files quickly, securely, and without any coding experience using BreezePDF.

Merge PDFs for Free →

However, many online PDF mergers present challenges, including privacy concerns and limitations on file size and features. Free online tools often come with usage restrictions or may compromise the confidentiality of your documents by storing them on their servers. This is why a local, more controlled solution is often preferred.

Python offers a powerful and flexible solution for merging PDFs, providing users with greater control over the process. Libraries like pypdf, PyMuPDF, pdfrw, and pikepdf allow for programmatic manipulation of PDF files, ensuring privacy and customization. For users seeking a simpler, no-code solution, BreezePDF.com offers a user-friendly alternative.

Why Use Python for PDF Merging?

Privacy is a significant concern when dealing with sensitive documents online. Many free online PDF mergers require uploading your files to their servers, raising questions about data security and confidentiality. Using Python scripts eliminates this risk by keeping all processing local, ensuring your documents remain on your device.

Free online PDF mergers often impose limitations on file size, the number of files you can merge, or the available features. These restrictions can be frustrating when dealing with large or complex merging tasks. With Python, you have the freedom to merge any number of files of any size, limited only by your system's resources.

Python scripts provide unparalleled flexibility and control over the PDF merging process. You can customize the merging order, exclude specific pages, and even manipulate the content of the PDFs before merging. This level of control is simply not available with most online tools.

Furthermore, Python's scripting capabilities allow for automation and integration with other tasks. You can create scripts that automatically merge PDFs based on specific criteria or integrate PDF merging into larger workflows. This automation can significantly streamline document processing tasks.

Libraries for Python PDF Merge

Several excellent Python libraries facilitate PDF merging, each with its strengths and weaknesses. These libraries allow you to programmatically manipulate and combine PDF documents with varying degrees of control and performance. Selecting the right library depends on your specific requirements and project scope.

pypdf (formerly PyPDF2)

pypdf is a versatile library that allows you to split, merge, crop, and transform PDF files. It is a pure Python library, making it easy to install and use across different platforms. pypdf is well-documented and widely used, making it a popular choice for basic PDF manipulation tasks.

Here's a basic code example using pypdf's `PdfWriter` class (formerly `PdfMerger`):

from pypdf import PdfWriter, PdfReader

merger = PdfWriter()

for filename in ["file1.pdf", "file2.pdf", "file3.pdf"]:
    reader = PdfReader(filename)
    merger.append(reader)

merger.write("merged.pdf")
merger.close()

The `append` method is used to concatenate PDF files. This simple method adds the entire content of one PDF to the end of another. It's a straightforward approach for merging entire documents sequentially.

pypdf also offers the `merge` method for more fine-grained control. This method allows you to insert pages from one PDF into specific locations within another. This provides greater flexibility in organizing the merged document. You can also merge PDFs in a directory by using the `os.listdir()` or `glob.glob()` as specified at add fillable fields to pdf article.

You can also select specific page ranges using `append(fileobj, pages=(start, stop))`. For example, `pages=(0, 3)` will include only the first three pages of the source document. Remember to always close files after processing to release resources and prevent errors.

Closing files after processing is crucial to prevent resource leaks and ensure data integrity. Failing to close files can lead to `ValueError: I/O operation on closed file` errors. Always include `file.close()` in your scripts.

PyMuPDF (fitz)

PyMuPDF is known for its speed and comprehensive features. It supports a wide range of PDF operations, including merging, splitting, and text extraction. PyMuPDF is a good choice for performance-critical applications.

PyMuPDF can also be used from the command line, for quick tasks without writing a script.

python -m fitz merge -o output.pdf file1.pdf file2.pdf file3.pdf

Here's a code example using `fitz.open()` and `insert_pdf()`:

import fitz

doc = fitz.open("file1.pdf")

for filename in ["file2.pdf", "file3.pdf"]:
    doc2 = fitz.open(filename)
    doc.insert_pdf(doc2)
    doc2.close()

doc.save("merged.pdf")
doc.close()

Note that in older versions, the method was called `insertPDF` instead of `insert_pdf`. Be aware of this difference when working with older code examples.

pdfrw

pdfrw is a pure Python library that excels at low-level PDF manipulation. It allows for precise control over PDF objects. However, it has limitations in handling bookmarks, annotations, and encryption, and is less actively maintained.

Here's a code example using `PdfReader` and `PdfWriter`:

from pdfrw import PdfReader, PdfWriter

writer = PdfWriter()

for filename in ["file1.pdf", "file2.pdf", "file3.pdf"]:
    reader = PdfReader(filename)
    writer.addpages(reader.pages)

writer.write("merged.pdf")

To exclude the last page, you can use slicing `writer.addpages(reader.pages[:-1])` when adding pages, which specifies all pages but the last.

pikepdf

pikepdf is a modern and actively maintained library built on top of QPDF. It offers a high-level API for manipulating PDF files. pikepdf is a good choice for projects requiring robust PDF handling capabilities. Its active maintenance ensures continued support and updates.

Here's a code example using `Pdf.new()` and `extend()`:

import pikepdf

merged = pikepdf.Pdf.new()

for filename in ["file1.pdf", "file2.pdf", "file3.pdf"]:
    pdf = pikepdf.Pdf.open(filename)
    merged.extend(pdf.pages)

merged.save("merged.pdf")

To exclude pages with pikepdf, you must iterate through the pages and exclude those that you want. You can do this by using a conditional statement within the loop. Each library offers a unique approach to PDF merging, allowing developers to choose the tool that best fits their project requirements.

Step-by-Step Guide to Merging PDFs with pypdf

pypdf is a popular library for Python PDF manipulation, known for its ease of use and comprehensive features. This section provides a detailed, step-by-step guide to merging PDFs using pypdf, including installation instructions and code examples. This should make it clear how to programmatically create a new merged PDF document.

Installation

To install pypdf, use pip, the Python package installer. Open your terminal or command prompt and run the following command: `pip install pypdf`. This will download and install the latest version of pypdf and its dependencies.

Consider using virtual environments to isolate your project dependencies. This practice ensures that your project uses specific versions of libraries without interfering with other projects on your system. Use `python -m venv .venv` to create one, and then activate it.

Basic Merging

Start by importing the necessary modules from the pypdf library: `PdfReader` and `PdfWriter`. These modules provide the classes needed to read and write PDF files.

Next, create a `PdfWriter` object. This object will hold the merged content. This is where the pages of the PDF files will be appended, and the new file is then written at the end.

Loop through the list of PDF file names that you want to merge. This ensures that each document you want to include in the final PDF is processed and added in the correct order. Create a variable to store the filename.

Open each PDF file in binary read mode (`'rb'`). This ensures that the file is read correctly, regardless of the platform or operating system. This step is essential for reading the PDF content.

Create a `PdfReader` object for each file. This object reads the content of the PDF file and allows you to access its pages. Assign the file object in binary read mode to it.

Iterate through the pages of each PDF file using the `PdfReader` object. For each page, append it to the `PdfWriter` object. This step adds the content of each page to the merged document in sequence.

Write the merged content to an output file using the `PdfWriter` object's `write()` method. Specify the name of the output file, such as "merged.pdf". This step saves the combined content to a new PDF file.

Finally, close the input file streams using the `close()` method. This releases the file resources and prevents potential errors. Ensure to close all opened file streams after use.

Merging PDFs in a Directory

To merge all PDFs in a directory, use `os.listdir()` or `glob.glob()` to get a list of PDF files. These functions provide a way to retrieve a list of files in a specified directory, allowing you to automate the merging process.

Here's an example code snippet for merging all PDFs in a directory:

import os
from pypdf import PdfWriter, PdfReader

merger = PdfWriter()

directory = "./pdfs"

for filename in os.listdir(directory):
    if filename.endswith(".pdf"):
        filepath = os.path.join(directory, filename)
        reader = PdfReader(filepath)
        merger.append(reader)

merger.write("merged.pdf")
merger.close()

Excluding Specific Pages

To exclude a blank or unwanted page, you can use conditional statements within the page iteration loop. This allows you to skip specific pages based on certain criteria, such as page number or content.

Here's a code example for excluding the last page:

from pypdf import PdfWriter, PdfReader

merger = PdfWriter()

for filename in ["file1.pdf", "file2.pdf", "file3.pdf"]:
    reader = PdfReader(filename)
    for i in range(len(reader.pages) - 1):
        page = reader.pages[i]
        merger.add_page(page)

merger.write("merged.pdf")
merger.close()

Merging Specific Page Ranges

Using the `pages` parameter in `append`, you can specify page ranges. You can include only specific page ranges, such as the first three pages or pages 1, 3, and 5.

Examples: `pages=(0, 3)` includes the first 3 pages. `pages=(0, 6, 2)` includes pages 1, 3, and 5. These ranges can be customized to fit the exact needs of your document merging task. This allows a highly customizable merge.

Advanced PDF Merging Techniques

Beyond the basic merging of PDFs, several advanced techniques can be employed to handle more complex scenarios. These techniques include handling encrypted PDFs, sorting and deduplicating files, dealing with large PDF files, and preserving bookmarks and annotations. These advanced techniques can be useful for professional workflows. Here's a look at some of them.

Handling Encrypted PDFs

To handle encrypted PDFs, use the `password` parameter in the `PdfReader` constructor. This allows you to open password-protected PDFs for merging if you have the correct password.

Consider the security implications when working with password-protected PDFs. Ensure that you handle passwords securely and avoid storing them in plain text. This is important to avoid unwanted access to sensitive information.

Sorting and Deduplication

Use dictionaries to sort or deduplicate files before merging. Dictionaries provide an efficient way to organize and filter files, allowing you to control the merging order and eliminate duplicates.

Here's a code example using a dictionary to sort by filepath or filename:

import os
from pypdf import PdfWriter, PdfReader

merger = PdfWriter()

directory = "./pdfs"

files = {}
for filename in os.listdir(directory):
    if filename.endswith(".pdf"):
        filepath = os.path.join(directory, filename)
        files[filepath] = filename  # Store filepath and filename

sorted_files = sorted(files.items())

for filepath, filename in sorted_files:
    reader = PdfReader(filepath)
    merger.append(reader)

merger.write("merged.pdf")
merger.close()

Dealing with Large PDF Files

Memory considerations are crucial when merging very large files. Loading entire PDF files into memory can lead to performance issues and crashes. Optimize your code to avoid these issues.

Potential solutions include using iterative processing to load pages in chunks or utilizing libraries optimized for memory management. This can help to reduce the memory footprint of your PDF merging tasks. A good alternative would be to just use the merging capability in BreezePDF.com to not have to worry about such things.

Preserving Bookmarks and Annotations

Preserving bookmarks and annotations during merging can be challenging. Some libraries may not fully support these elements, resulting in their loss during the merging process. This is why you should use a tool that retains the PDF attributes.

Explore tools and techniques for handling bookmarks and annotations. This may involve using specialized libraries or manual manipulation of PDF objects to ensure their preservation during merging. Preserving bookmarks and annotations is important for maintaining the structure and usability of the merged document.

Using `pdfmerge` command line utility

Some libraries provide a command-line utility named `pdfmerge` for easy pdf merging. These utilities are often more performant than code based solutions.

The common usage parameters are `password`, `output`, and `page selection`. These utilities have the basic features you would want in a python command line tool.

The command line utilities are often coupled with a Python module. This module provides an easy, high-level interface to use within your Python project.

BreezePDF as a Solution

BreezePDF offers a convenient alternative to coding for PDF merging. It provides a user-friendly interface that simplifies the merging process. You are able to add input boxes to PDFs by clicking the input box icon, dragging it where you want and dragging it around if needed.

BreezePDF's online accessibility and ease of use make it an ideal solution for users who prefer a no-code approach. You can access BreezePDF from any device with a web browser, eliminating the need for software installations or complex configurations. You are also able to type on the PDF by clicking the letter icon, clicking where you want to type, and after done typing text can drag around as needed and adjust color and font size.

Moreover, BreezePDF prioritizes security and privacy. Your documents are never sent to a server; all processing happens in your browser, ensuring that your data remains private and secure. If you need to add a signature, just click the scribble icon, draw a signature, click "insert" and drag it to your desired location!

Troubleshooting and Common Issues

When working with PDF merging, you may encounter various issues. These can include character encoding errors, deprecated functions, file closing problems, and broken internal links. Addressing these issues is essential for ensuring a smooth merging process.

Handling `PdfReadError: Illegal character error`

To handle `PdfReadError: Illegal character error`, use `strict=False` when creating the `PdfReader` object. This tells the library to ignore minor errors in the PDF file format and continue processing.

Addressing Deprecated Functions

Be aware of deprecated functions, such as `PdfFileMerger` vs. `PdfMerger` vs. `PdfWriter`. Use the current recommended functions to ensure compatibility and avoid errors. Use up-to-date syntax and methods, and replace deprecated functions when necessary.

Ensuring Proper File Closing

Ensure proper file closing to avoid `ValueError: I/O operation on closed file`. Always close file streams after processing to release resources and prevent errors. Failing to do so can lead to file access issues.

Resolving Issues with Internal Links Not Working

Resolving issues with internal links not working can be complex. This may require manual manipulation of PDF objects to ensure that the links are preserved during merging. Check the links in the final PDF.

Python Interpreter Issues (conda envs)

Python interpreter issues, such as those related to conda environments, can cause problems. Ensure that you are using the correct environment and that all necessary libraries are installed. Activate your conda environment.

Optimizing Performance

Performance is a key consideration when choosing a PDF merging library. Different libraries offer varying levels of performance, and selecting the right one can significantly impact the speed of your merging tasks. The time it takes to combine the files can differ with each library used.

Time Comparison of Different Libraries

A time comparison of different libraries (pypdf, PyMuPDF, pdfrw) can help you determine which library is best suited for your needs. PyMuPDF is generally faster than pypdf for large files. Evaluate the performance of each library based on your specific use case and file sizes.

Recommendations for Choosing the Right Library

Consider the complexity of your merging tasks and the size of your PDF files when choosing a library. For basic merging tasks, pypdf may be sufficient. For more complex tasks or large files, PyMuPDF or pikepdf may be better choices. Also, remember that BreezePDF is always a convenient browser based option.

General Tips for Optimizing Python Code

General tips for optimizing Python code for PDF merging include using efficient data structures, minimizing memory usage, and leveraging optimized library functions. Profiling your code can help identify bottlenecks and areas for improvement. Ensure to use only the code you need for optimum speeds.

Conclusion

Using Python for PDF merging offers numerous benefits, including enhanced privacy, greater flexibility, and the ability to automate complex tasks. Libraries like pypdf, PyMuPDF, pdfrw, and pikepdf provide the tools necessary to manipulate and combine PDF files programmatically. As you can see, it can be a complex process.

However, for users seeking a simpler and more user-friendly solution, BreezePDF provides an excellent alternative. With its intuitive interface and online accessibility, BreezePDF simplifies the PDF merging process without compromising security or privacy.

We encourage you to explore BreezePDF for your PDF merging needs and experience the ease and convenience of a no-code solution. Get started today and simplify your document management tasks! You can also password protect the PDF, just click the lock icon, insert pdf password, click 'Apply'. pdf will be automatically password protected when you download it. if afterwards you decide you don't want to password protect it, simply click lock icon again to remove password from pdf.