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.

Tags

#Python, #PDF, #PyPDF2 ,#PDFmerge, #PDFmanipulation, #PDFprocessing

Similar Programs Chapter Last Updated
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
Python Program To Schedule A Job To Run Every Hour Python 10-08-2023
Python Script For Scheduling Jobs Python 09-08-2023
Python String To Datetime Python 23-07-2023
Python Date Comparison Python 23-07-2023
Python Date Now Python 23-07-2023
Python Date to String Python 23-07-2023
How to get file creation and modification date or time in Python Python 19-06-2023
How do you find the difference in time in Python Python 19-06-2023
How To Get The Current Time In Different Time Zones Using Python Python 19-06-2023
Python Get Current Date And Time Python 19-06-2023
Python Program That Schedules A Task To Run At A Specific Time Python 10-06-2023
Python Program To Replace Characters In A String Python 03-06-2023
Python Program To Replace Blank Space With Hyphen Python 30-05-2023

1 2 3 4