How to Merge Two PDF Files Using Python
Chapter:
Python
Last Updated:
12-03-2023 13:11:15 UTC
Program:
/* ............... START ............... */
import PyPDF2
# Define the PDF files to join
pdf_file_1 = open('file1.pdf', 'rb')
pdf_file_2 = open('file2.pdf', 'rb')
# Create a new PDF writer object
pdf_writer = PyPDF2.PdfFileWriter()
# Loop through each PDF file and add its pages to the writer object
for pdf_file in [pdf_file_1, pdf_file_2]:
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
# Write the merged PDF file to disk
pdf_output_file = open('merged_file.pdf', 'wb')
pdf_writer.write(pdf_output_file)
# Close the input and output files
pdf_file_1.close()
pdf_file_2.close()
pdf_output_file.close()
/* ............... END ............... */
Notes:
-
PyPDF2 library, which provides tools for working with PDF files in Python.
- First, we import the PyPDF2 library, which provides tools for working with PDF files in Python.Then, we open the two input PDF files ('file1.pdf' and 'file2.pdf') in binary mode using the open() function and store the resulting file objects in variables pdf_file_1 and pdf_file_2.
- Next, we create a new PdfFileWriter object from PyPDF2, which we will use to write the merged PDF file.We loop over the two input PDF files, creating a new PdfFileReader object for each file and using it to loop over the pages in the file.
- For each page, we add it to the PdfFileWriter object using the addPage() method.
- Finally, we write the merged PDF file to disk as 'merged_file.pdf' using the write() method of the PdfFileWriter object. We also close all input and output files using the close() method of each file object.
- In summary, this program reads two input PDF files, combines their pages into a single PdfFileWriter object, and then writes the merged PDF file to disk.