In this tutorial, we are going to learn how to merge PDF files using #PyPDF2 in Python.
Portable Document Format (PDF) is probably the most widely used file format when it comes to creating document today. By default, most PCs don’t come with PDF files merge/combine feature, and if your #PDF file has sesnitive information, you are running into security risk when using free online tools or 3rd party software. So creating your own Python program to perform PDF files merge task is the safest solution, and the script only takes less than 2 minutes to write.
Before we dive into tutorial, you will need to install PyPDF2 library (pip install PyPDF2).
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
import os from PyPDF2 import PdfFileMerger source_dir = os.getcwd() merger = PdfFileMerger() for item in os.listdir(source_dir): if item.endswith('pdf'): merger.append(item) merger.write('./Output/Lecture Complete.pdf') merger.close()
I want to generate PDF files from an excel file. After this coding runs, the result is that I will have multiple PDF files with the suffix “-index.pdf” – (e.g., 875458-indexl.pdf) I can change the suffix to anything.
And I will extract another set of PDF files from a zip file that have the identical file name as 875458.pdf.
At the end, in one folder, there will be multiple pairs of files as below.
My goal is to find a way to merge each pair of files into one file.
The end result would be as below.