In this tutorial, I am going to Merge two PDFs into single in Python using PyPDF2 which is a Python library PyPDF2 is a free and open-source pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files, It can also add custom data, viewing options, and passwords to PDF files. PyPDF2 can retrieve text and metadata from PDFs as well.
Requirements
- PyPDF2
To get started make sure you have PyPDF2 installed in your system. If you don’t have these you have to install them using PIP which is a package manager for Python that can install Python modules or packages.
Using these following commands you can install PyPDF2 in your system
pip install PyPDF2
Code
Here in this code I am going to save pdfwriter() class in to a variable and add the two pdfs document to output, which will append entire p document to the end of the output document, then write to an output PDF document and close file descriptors.
#importing module
from PyPDF2 import PdfWriter
# save pdfwriter() class in to a variable
merge = PdfWriter()
# Add the two pdfs document to output
files = ["I AM.pdf","I AM2.pdf"]
# append entire p document to the end of the output document
for p in files:
merge.append(p)
# Write to an output PDF document
merge.write("marge_pdf.pdf")
# Close File Descriptors
merge.close()
Output
Before:

After:
