Merge PDFs Using Python
By annak
- 2 minutes read - 217 wordsMerge PDFs Using Python
Working with PDF files can a pain…but it doesn’t have to be. You don’t even need any expensive software to handle PDFs: just a few lines of Python code will suffice.
In this quick tutorial I’ll show you how to easily merge multiple files without having to tinker in the Adobe Acrobat or a similar program, simply taking advantage of the capabilities of the PyPDF2 library. This method is much quicker and cheaper and all the tools used here are free.
I’m using Python version 3.
Tip! Some basic Python knowldege required!
- Gather all PDFs you want to merge in the same folder. You won’t have to specify the full path for each file.
- Install the PyPDF2 package
pip install PyPDF2
. - Import PyPDF2 python library and PdfMerger class:
import PyPDF2
from PyPDF import PdfMerger
- Create an object to hold the PdfMerger class.
merger = PdfMerger()
- Loop over the PDF files. Use
append
method to attach files in the consecutive order.
for pdf in ["file1.pdf", "file2.pdf", "file3.pdf", "file4.pdf"]:
merger.append(pdf)
- Create a new PDF file using
write()
method on the merger object:
merger.write("my-new-merged-file.pdf")
- To close the program, use the
close()
method:
merger.close()
The resulting Python code:
import PyPDF2
from PyPDF import PdfMerger
merger = PdfMerger()
for pdf in ["file1.pdf", "file2.pdf", "file3.pdf", "file4.pdf"]:
merger.append(pdf)
merger.write("my-new-merged-file.pdf")
merger.close()