Python PikePDF – Merge multiple PDF files

In this post we will be creating a Python script that will merge the contents of multiple PDF files. To work with PDF files and achieve our goal, we’ll utilize the PikePDF library designed specifically for this purpose. Its simple yet powerful functions enable easy merging, splitting, and manipulation of PDF documents.

See the snippet of Python code below where we use the PikePDF library to merge the contents of multiple PDF files. We start by creating a list of the PDF files that we want to merge. In this case, the list contains two files: “file1.pdf” and “file2.pdf”. We also create an empty PDF document that will eventually hold the merged PDF files.

A for loop is then used to iterate through each PDF file in the list of PDFs to merge. Using a with statement we open the current PDF file using PikePDF’s ‘open()’ function, which returns a pdf object representing the PDF. Using the ‘extend()’ method we append the pages of the current PDF file to the merged_pdf object created earlier. Finally, the ‘save()’ method is used to save the merged PDF file to disk with the name “merged_file.pdf”.

import pikepdf

pdfs_to_merge = ["file1.pdf", "file2.pdf"]
merged_pdf = pikepdf.Pdf.new()

for pdf_file in pdfs_to_merge:
    with pikepdf.open(pdf_file) as pdf:
        merged_pdf.pages.extend(pdf.pages)

merged_pdf.save("merged_file.pdf")

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply