Build a Simple PDF Merger in Python

I’m a versatile writer exploring technology, science, and the unexpected. Join me on a journey through fresh ideas and surprising insights.
At some point, you’ve probably ended up with too many PDF files, receipts, assignments, timetables, certificates and thought, “Can I just have one file, please?”
Well, yes. You can.
And you can build your own PDF merger using Python in under 10 minutes.
In this short guide, we’ll create a script that:
Scans a folder for all
.pdffilesMerges them automatically
Saves the combined version in the same folder
No manual picking. No dragging. Just one command.
What We’ll Use
We’ll be using a library called PyPDF2 — a lightweight and easy-to-use Python library for reading, writing, and merging PDFs.
Install it using pip:
pip install PyPDF2
That’s all you need. No extra dependencies, no complex setup.
The Script
Here’s the full working code:
import os
from PyPDF2 import PdfMerger
def merge_pdfs_in_folder(folder_path):
# Ensure the folder exists
if not os.path.isdir(folder_path):
print("❌ Folder not found. Please check the path.")
return
# Collect all PDF files in the folder
pdf_files = [f for f in os.listdir(folder_path) if f.lower().endswith('.pdf')]
pdf_files.sort() # Keeps them in alphabetical order
if not pdf_files:
print("⚠️ No PDF files found in the folder.")
return
# Create a merger object
merger = PdfMerger()
print(f"🧩 Found {len(pdf_files)} PDF files. Merging now...")
for pdf in pdf_files:
file_path = os.path.join(folder_path, pdf)
print(f"➡️ Adding: {pdf}")
merger.append(file_path)
output_path = os.path.join(folder_path, "merged.pdf")
merger.write(output_path)
merger.close()
print(f"\n✅ Merged PDF saved as: {output_path}")
if __name__ == "__main__":
folder = input("📁 Enter the path to your folder containing PDF files: ").strip('"')
merge_pdfs_in_folder(folder)
How It Works
User Input:
The script asks for your folder path, e.g.C:\Users\Samuel\Documents\TimetablesFile Detection:
It scans the folder for every.pdffile inside.Sorting:
It sorts them alphabetically before merging — soA.pdfcomes beforeB.pdf.Merging:
Each file is appended in order using PyPDF2’sPdfMerger.Saving:
The output is written to the same folder asmerged.pdf.
Example Output
If your folder contains:
Samuel_Weekday_Timetable.pdf
Samuel_Saturday_Timetable.pdf
Samuel_Sunday_Timetable.pdf
You’ll get:
merged.pdf
A single clean document that holds all your timetables in order.
Why This Project Is Nice
This tiny project shows automation in its simplest form:
You use Python to replace a repetitive manual task.
You learn file handling and sorting.
You get a real, useful tool.
It’s perfect for beginners who want a quick confidence boost or an idea for a weekend project to post on GitHub or Medium.
Next Steps (If You Want to Upgrade)
Here are a few ideas to take it further:
Add a GUI using
tkinter
So you can click “Select Folder” and “Merge PDFs” instead of typing the path.Custom Output Names
Let users type what the merged file should be called.Drag-and-Drop CLI
So you can just drag a folder onto the script file, and it does the rest.Add Notifications or Sounds
Use libraries likeplyerorwinsoundto alert when merging completes.
Final Thoughts
Sometimes, the best coding projects are the ones that fix your own tiny problems.
This PDF Merger is one of those quick to build, instantly useful, and totally customisable.


