PyPDF2 is a PDF toolkit which simplifies working with PDF documents in Python. Although the library exposes a good deal of useful functionality, this post focuses on how to merge two or more PDFs into a single document using functionality avilable in the PdfFileMerger class.

Assume a directory containing several PDFs documents is to be compiled into a single document. With PyPDF2, it's as simple as instantiating and instance of the PdfFileMerger class and calling its append method. We demonstrate below:

import os
import os.path
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter

# Directory containing files to merge.
pdf_dir = "PDFs"

# Generate list of files for merging.
pdf_files = [os.path.join(pdf_dir, ii) for ii in os.listdir(pdf_dir) if ii.endswith(".pdf")]

merger = PdfFileMerger()
for ii in pdf_files:
    merger.append(PdfFileReader(open(ii, 'rb')))

# Write merged PDF to file.
merger.write("merged.pdf")

# Close merge instance.
merger.close()