Python: Merge PDF Documents

A large number of scattered but content-related PDF documents will be very inefficient to deal with and difficult to manage. Merging them into a single PDF file is a good solution. By consolidating these files, not only can you streamline your document handling process, but you can also share and view them conveniently, significantly increasing efficiency. This article will show how to merge PDF files in Python programs using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Merge PDF Documents in Python

Spire.PDF for Python provides the PdfDocument.MergeFiles() method to easily merge multiple PDF documents into one PDF document. The detailed steps are as follows:

  • Create a list of the PDF file paths.
  • Merge the PDF files using Document.MergeFiles(inputFiles: List[str]) method.
  • Save the merged document using PdfDocumentBase.Save(filename: str, FileFormat.PDF) method.
  • Python
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()

Python: Merge PDF Documents

Merge PDF Documents by Cloning Pages

Merging PDF files can also be done by cloning pages from PDF files to a new PDF file using PdfDocument.AppendPage(PdfDocument) method. The detailed steps are as follows:

  • Create a list of the PDF file paths.
  • Load each PDF document as an object of PdfDocument class and add them to a list.
  • Create an object of PdfDocument class to create a new PDF document.
  • Loop through each loaded PDF document and insert their pages to the new PDF document using PdfDocument.appendPage() method.
  • Save the new PDF document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf 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 pages of the loaded PDF documents into the new PDF document
for pdf in pdfs:
    newPdf.AppendPage(pdf)

# Save the new PDF document    
newPdf.SaveToFile("output/ClonePage.pdf")

Merge Selected Pages of PDF Documents

Spire.PDF for Python also allows users to select and insert pages from one PDF document to another using PdfDocument.InsertPage() method and PdfDocument.InsertPageRange() method, enabling the merging of specified PDF pages. The detailed steps are as follows:

  • Create a list of the PDF file paths.
  • Load each PDF document as an object of PdfDocument class and add them to a list.
  • Create an object of PdfDocument class to create a new PDF document.
  • Insert the selected pages of the loaded documents into the new PDF document using PdfDocument.InsertPage(PdfDocument, pageIndex: int) method and PdfDocument.InsertPageRange(PdfDocument, startIndex: int, endIndex: int) method.
  • Save the new PDF document using PdfDocument.SaveToFile() method.
  • Python
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")

Python: Merge PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.