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 Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023

1 2 3 4