We can merge multiple pdf files using Python. Pdf merging using python code is very easy to execute and user friendly to edit. Python code is given below for ready use.

This is very common these days to merge multiple pdf files into one pdf and we generally perform this task using online websites.

What do we generally do?

Google Serach
  • Step 1: We open google.com and write “merge pdf online” in the search bar.
  • Step 2: Open any website suggested.
  • Step 3: Upload our files to this website.
  • Step 4: Arrange files according to the desired sequence.
  • Step 5: Click on merge and download.

What is alarming?

  • Is your file safe while uploading on those websites?
  • What is going on in the backend while merging?
  • Is the website deleting your files after merging?

Think about your confidential data once before uploading anywhere.

How to save your confidential data while merging?

Option 1: You can purchase a license of some genuine software by paying money.

Option 2: You can run 6 lines of python code and your work will be done at no cost.

Note: If you are choosing option 1, you can leave this article but if you are going with option 2 then you should stay with the article to understand the whole picture.

No Python knowledge required.

Just follow below steps and do this task.

About the solution

I have written 6 lines code in python, which will provide the safest way to perform this task on your computer without uploading your document anywhere.

It will really help you to save your confidential documents from different fraudulent websites.

Solution in a Nutshell below, Just Follow

  1. Install Python.
  2. Install PyPDF2 [Library used for PDF merging].
  3. Copy the code written below and change your file name of yours.
  4. Run the code and get the merged file.

1. Install Python

Go to the official website of Python [https://www.python.org/downloads/]

You can also click below to get Python’s latest version.

Download Python and Install [Just do next and next and next and it will successfully install on your machine with the default path]

Latest Version [Date : 30 Nov 2022]

username will be your “user name” in the below-given path.

Default Location for Windows OS as below:


2. Install PyPDF2 [Library used for PDF merging].

We will use an additional python package “PyPDF2” to perform this pdf merging task.

PyPDF2 is a free and open-source pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files.

It can also add custom data, viewing options, and passwords to PDF files. PyPDF2 can retrieve text and metadata from PDFs as well.

PyPDF2 is not the part of python standard library so we will install this package.

Please follow the below steps to add the PyPDF2 library.

Step 1: Go to the location where python is installed (Default Path given below)


Python Location

Step 2: Go to the Scripts folder and you will get two pip version



Run the command prompt from this folder and write the below command to install the PyPDF2 library.

pip3 install PyPDF2

You will get messages after installation “Successfully installed pypdf2-2-10.3”

We can check the list of all installed libraries by the command below

pip3 list

We will get this type of list shown below to confirm this installation.

Check Version

3. Code for pdf files merging task


Please copy the above 6 lines of code given below in italic font, paste it into notepad++ or any editor you are using and save it with the extension .py [Test.py]

My file name is ‘Test.py’ but you can change it as you like.

Now copy below 6 lines in italic font:

from PyPDF2 import PdfMerger

inputfiles = [‘file01.pdf’, ‘file02.pdf’,’file03.pdf’,’file04.pdf’]

output = PdfMerger()

for pdf in inputfiles:



Explanation of the code below

Line 1: from PyPDF2 import PdfMerger

Import PdfMerger module from PyPDF2 library

Line 2: inputfiles = [‘file01.pdf’, ‘file02.pdf’,’file03.pdf’,’file04.pdf’]

Suppose you have 4 files to merge [Please modify the files names according to your file name in code]

I have taken 4 files with the below name:

1.    file01.pdf

2.    file02.pdf

3.    file03.pdf

4.    file04.pdf

Note: Just write these file names with extensions in the sequence in the code, you want to see in the merged output file.

Rest 4 lines of code are written to merge these files and to create an output file by the name “MergedFile.pdf”

 Note: you can also change the output file name according to your test.

4. Run the code and get the merged file.

Put the files as shown below in a folder [Code saved with the name “Test.py”]

File Structure

Step 1: Go to the folder where you have placed these ‘Test.py’ and raw pdf files.

Note: I have placed these files in ‘Mycode’ folder.

Step 2: Run the command prompt from there or you can run ‘cmd’ from the windows search bar and change the directory accordingly.

C:\Users\username\AppData\Local\Programs\Python\Python310\Mycode>python Test.py

Please Press Enter and the code will run successfully and the merged file will generate in the same folder.

Merged File

If you are getting any errors, please check the set environment variable for python.

Note: we should add the python.exe path in the environment variable and name it python. Now we can run python code from any location by just writing the below commands:

  • python Test.py

Note: You can also do splitting, merging, cropping, and transforming those pdf pages using the PyPDF2 library so please explore as per your need.


Try the above steps and good to go with this code for sure. If you are facing any issue with this code or your system is throwing any error please comment or write us.

This is the safest way to merge pdf files using Python without uploading your confidential document to any online website or server. See you guys in another article.

Home Page - Basic Engineer
Home Page – Basic Engineer

Stay Safe, Stay Connected. Thank You

Rahul Verma

17+ years of experience in software development, IoT, telecom, banking, finance, embedded systems, data science, machine learning, and artificial intelligence with multiple market leaders worldwide. I hope you found this blog post informative and helpful. Cheers!


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *