You've got three PDFs — a contract, an appendix, and a cover page — and you need them as a single file. You could fire up a GUI tool, drag-and-drop, wait for the spinner, and hope the page order didn't get scrambled. Or you could write a five-line Python script that does it reliably, every time, in a CI pipeline, a cron job, or a backend service.
Spire.PDF for Python lets you create, edit, and merge PDF documents entirely from Python code. No Java dependency, no COM automation, no Adobe Acrobat on the machine. Just pure Python with a full-featured PDF engine under the hood.
In this article, you will learn how to:
- Install Spire.PDF for Python via pip
- Merge two PDF files into one document
- Combine an arbitrary number of PDFs from a folder
- Insert pages from one PDF into the middle of another
- Append content to an existing PDF without overwriting
Why merge PDFs in Python
Merging PDFs is one of those tasks that seems trivial until you need to do it at scale. A single pair of files? A desktop tool handles it. But what about:
- Batch processing — hundreds of invoices that need to be combined by month
- Automated reports — a cover page + generated charts + appendix, assembled on the fly
- CI/CD pipelines — a build step that packages documentation artifacts
- Serverless functions — a Lambda handler that merges uploaded chunks into a single deliverable
Python gives you the control and expressiveness to automate all of these without leaving the language.
Merge PDF files
The MergeFiles() method combines all PDFs in the list into a new PDF document object.
from spire.pdf.common import *
from spire.pdf import *
# Create a list of the PDF file paths
inputFile1 = "Sample1.pdf"
inputFile2 = "Sample2.pdf"
inputFile3 = "Sample3.pdf"
files = [inputFile1, inputFile2, inputFile3]
# Merge the PDF documents
pdf = PdfDocument.MergeFiles(files)
# Save the result document
pdf.Save("output/MergePDF.pdf", FileFormat.PDF)
pdf.Close()
Merge Selected Pages from PDFs in Python
In some cases, you may only want to merge specific pages of multiple PDFs. Spire.PDF for Python supports to select pages from different PDF documents and insert them into a new PDF file.
from spire.pdf import *
from spire.pdf.common import *
# Create a list of the PDF file paths
file1 = "Sample1.pdf"
file2 = "Sample2.pdf"
file3 = "Sample3.pdf"
files = [file1, file2, file3]
# Load each PDF file as an PdfDocument object and add them to a list
pdfs = []
for file in files:
pdfs.append(PdfDocument(file))
# Create an object of PdfDocument class
newPdf = PdfDocument()
# Insert the selected pages from the loaded PDF documents into the new document
newPdf.InsertPage(pdfs[0], 0)
newPdf.InsertPage(pdfs[1], 1)
newPdf.InsertPageRange(pdfs[2], 0, 1)
# Save the new PDF document
newPdf.SaveToFile("output/SelectedPages.pdf")
Batch Processing: Merge Multiple PDF Files in a Folder
The Python script loops through each source PDF in a specified folder, then appends all pages from the source PDFs to a new PDF file.
import os
from spire.pdf.common import *
from spire.pdf import *
# Specify the directory where the source PDFs are stored
folder = "pdf_folder/"
# Create a new PDF to hold the combined content.
merged_pdf = PdfDocument()
# Loop through each source PDF
for file in os.listdir(folder):
if file.endswith(".pdf"):
pdf = PdfDocument(os.path.join(folder, file))
# Appends all pages from each source PDF to the new PDF
merged_pdf.AppendPage(pdf)
pdf.Close() # Close source PDF
# Save the merged PDF after processing all files
merged_pdf.SaveToFile("BatchCombinePDFs.pdf")
merged_pdf.Close() # Release resources
Merging PDF files in Python helps build server-side document pipelines, avoid manual PDF editors, and create reusable automation scripts for data and document workflows.

Top comments (0)