pyPDF2: merging two PDF files

Posted by Afsal on 20-Oct-2023

Hi Pythonistas!

In the previous post we have learned about the basics of pyPDF2. Today we are going to learn how to merge 2 PDF files using pyPDF2. Let us dive into the code.

Open two pdf files to merged

import PyPDF2

pdf1 = open('sample.pdf', 'rb')
pdf2 = open('sample2.pdf', 'rb')

pdf_reader1 = PyPDF2.PdfReader(pdf1)
pdf_reader2 = PyPDF2.PdfReader(pdf2)

We have opened two files using the PdfReader method.

Create PDF writer object

pdf_writer = PyPDF2.PdfWriter()

This will create a new PDF writer object.

Adding pages to writer object

for page in pdf_reader1.pages:
    pdf_writer.add_page(page)

for page in pdf_reader2.pages:
    pdf_writer.add_page(page)

Here we are looping the pages in pdf files and adding this page to write objects using add_page method.

Create new files and write the content

merged_pdf = open('merged.pdf', 'wb')

pdf_writer.write(merged_pdf)

Close all the files

pdf1.close()

pdf2.close()

merged_pdf.close()

Completed code

import PyPDF2

pdf1 = open('sample.pdf', 'rb')
pdf2 = open('sample2.pdf', 'rb')

pdf_reader1 = PyPDF2.PdfReader(pdf1)
pdf_reader2 = PyPDF2.PdfReader(pdf2)

pdf_writer = PyPDF2.PdfWriter()

for page in pdf_reader1.pages:
    pdf_writer.add_page(page)

for page in pdf_reader2.pages:
    pdf_writer.add_page(page)

merged_pdf = open('merged.pdf', 'wb')
pdf_writer.write(merged_pdf)

pdf1.close()
pdf2.close()
merged_pdf.close()

Once the code is executed a file named merged.pdf will be generated in the current folder. In the upcoming post we will learn how to rotate the pdf files using pyPDF2

I hope you have learned something from this post please share your valuable suggestions with afsal@parseltongue.co.in