Merge PDF File Using Python and PyPDF2 Library

Hari
2 min readNov 15, 2023

--

Hi, this is my first post on Medium. Now prepare yourself by opening your Jupiter notebook or Google Colab cause we will use Python in this tutorial. and let's get started. Have you ever found yourself in a situation where you needed to merge multiple PDF files into a single document? Perhaps you’ve explored online PDF tools like I Love PDF or similar services. While these platforms offer a good solution we don't need to install software to help you merge pdf. In this tutorial, We will use Python programming and explore how to merge PDFs seamlessly using the PyPDF2 library.

  1. Install the library using this command.
pip install PyPDF2

PyPDF2 is a Python PDF library available for free and open-source, and it can split, merge, crop, and alter the pages of PDF files. Passwords, custom data, and viewing options can also be added to PDF files. Text and metadata can also be extracted from PDFs using PyPDF2.

2. Import the library and write the function

import PyPDF2

#Define Function
def merge_pdfs(input_pdf, output_destination):
merger = PyPDF2.PdfMerger()

for pdf in input_pdf:
merger.append(pdf)

output_path = output_destination
merger.write(output_path)
merger.close()

In PyPDF version 3.0.0 PdfFileMerger() is depreciated so we will use PdfMerger()

3. Perform Merge PDF File

# Set your input folder that containing pdf file
input_pdf = ['input/file1.pdf','input/file2.pdf','input/file3.pdf']
# Set your output folder and desired output filename
output_destination = "output/merged.pdf"

# Call the function
merge_pdfs(input_pdf, output_destination)

I have 3 files in the input folder and a folder named output as the destination for the merged files. The files must be sorted correctly because the merge process will start from the first array to the last (ascending). I use Google Colab and that's connected to My Google Drive as my workspace. So I can open and run it everywhere.

Folder Structure

Pdf Input

PDF Output

I hope this tutorial is useful for you and not confusing, sorry if there are errors in this article.

--

--

Hari
0 Followers

I'am Hari, fullstack programmer who is currently interested in learning machine learning and artificial intelligence.