in this python pdf tutorial, We’ll cover pdf merging functionality using PyPDF2.You can merge one or more pdf files into a single pdf file. We’ll use PdfFileMerger class to merge pdf files.
You can also checkout other python file tutorials:
- How To Load Json file Using Python
- How To Create and Write JSON file in Python
- How To Read Write Yaml File in Python3
- Write Text File Using Python 3
- Extract Text From PDF File Using Python
How To Merge PDF Files in Python
We’ll use the PyPDF2 python module for pdf manipulation. So first, We’ll install this into the python application.
Install pypdf2 in python
To use the PyPDF2 library in Python, We need to first install PyPDF2. Run the below code to install the PyPDF2 module in the system.
pip install PyPDF2
How To Combine PDF Files In Python
The below python script help to merge two pdf files into one file.
import PyPDF2 def main(): # pdf files to merge all_files = ['first.pdf', 'second.pdf'] # output pdf file name output_file = 'combined.pdf' # merge object pdfMerger = PyPDF2.PdfFileMerger() for pdf in all_files: pdfMerger.append(pdf) # writing combined pdf to output file with open(output_file, 'wb') as f: pdfMerger.write(f) if __name__ == "__main__": # calling the main function main()
The above code will combine first.pdf
and second.pdf
file output into the new pdf(combined.pdf) file.
Let’s have a look at some of the program’s key features:
- Step 1: Define all pdf files that we need to merge.
- Step 2: Define output files that ll hold all pdf files content after merge.
- Step 3: We create an object pdfMerger of pdf merger class Using
PdfFileMerger
. - Step 4: Iterate on all pdf list and append file object of each pdf to pdf merger object using
append()
method. - Step 5: Finally, We’ll write the pdf pages to the output pdf(
combined.pdf
) file using write method of pdf merger object.