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.
Similar Programs Chapter Last Updated
Python Date Difference In Days Python 01-04-2023
Bellman Ford Algorithm In Python Python 25-03-2023
Python Program To Display Current Date And Time Python 24-03-2023
Binary Search Tree Implementation Python Python 21-03-2023
Python Program To Check Palindrome Number Python 12-03-2023
Python Tuples Example Python 19-11-2021
Python Iterator Example Python 12-11-2021
Python Lambda Functions Python 11-11-2021
Integer To String In Python Python 22-10-2021
Python Datetime Format Python 21-10-2021
Range Function In Python | Python range () Python 11-10-2021
Python Desktop Notification Popup In Linux Python 10-07-2021
Python JSON Parser Example | How To Parse JSON In Python Python 25-06-2021
Python PIP | How To Install Packages In Python Python 16-06-2021
How To Access Python Dictionary Python 14-06-2021
Dictionary In Python Examples Python 10-06-2021
Shortest Path Algorithm In Python Python 23-02-2021
Merge Sort In Python Python 01-12-2020
Kruskal Algorithm In Python Python 28-11-2020
Python Mysql Connector Example Python 21-11-2020
Add To Set Python Python 05-10-2018
Set In Python Python 05-10-2018
List In Python Python 29-09-2018
Integer To String Python Python 29-09-2018
Python Variables Python 21-09-2018
Python String Contains Python 15-09-2018
String In Python Python 22-09-2018
Python Switch Statement Python 20-09-2018
Python Function Example Python 14-09-2018
Python If Statement Python 14-09-2018

1 2