Merging multiple PDF files in Python

Nidhi Gupta
1 min readAug 2, 2021

PDF and Word documents are binary files, which makes them much more complex than plaintext files. There are Python modules that make it easy for us to interact with PDFs and Word documents.

First Python module: PyPDF2

Second Python module: Python-Docx

To merge multiple pdf files into one pdf file python makes use of a module called PyPDF2 or Python-Docx.

In this article, we will use module PyPDF2

Step:-1 Create a folder named PyPDF, add a file app.py, open the terminal and execute the following command

# Install PyPDF2 module

pipenv install PyPDF2

Step:-2 Open app.py and import the PyPDF2 module.

# import PyPDF2

import PyPDF2

Step:-3 Add the following lines of code

PdfFileMerger:- It is the class from the PyPDF2 module that is widely used to merge multiple PDF files into one in Python.

merger = PyPDF2.PdfFileMerger()

# add names of all the pdf files in the array pdf_names

pdf_names = [“pdf1.pdf”, “pdf2.pdf”]

for pdf_names in pdf_names:

merger.append(pdf_names)

# after merging all the pdf files give a new pdf file name which will write all the #content from all the different pdf.

#here merged.pdf contains content from pdf1 and pdf2

merger.write(“merged.pdf”)

Do clap! if find useful.🙂

--

--

Nidhi Gupta

Azure Data Engineer 👨‍💻.Heading towards cloud technologies expertise✌️.