Back to the blog

GUI to Merge PDFs with PyQt5

Information is the most valuable asset for any person or company. Critical or sensitive information should not travel across the Internet "just to merge a couple of PDFs".

When you use a "free" online service, you're actually paying with the data you upload: you don't know if files are deleted from the server, sold, or used for analytics. The solution is to process everything locally — here, a desktop app with PyQt5 + PyPDF2 that merges PDFs without touching the network.

The app has a simple interface with two columns: on the left, the file selector and list of loaded files; on the right, buttons to merge + save and delete from the selection.

Merge PDF GUI

import sys
import os
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget,
                             QHBoxLayout, QVBoxLayout, QLabel, QListWidget, QFileDialog)
from PyQt5.QtCore import Qt
import PyPDF2

# Functions of the buttons
all_file_paths = []
file_paths = None
def select_files():
    global file_paths
    dialog = QFileDialog()
    file_paths, _ = dialog.getOpenFileNames(caption = 'Select PDF files', directory = '/', filter = 'PDF files (*.pdf)')
    file_names = [os.path.basename(path) for path in file_paths]

    for file, path in zip(file_names, file_paths):
        if not any(file == list_wid.item(i).text() for i in range(list_wid.count())):
            list_wid.addItem(file)
            if path not in all_file_paths:
                all_file_paths.append(path)

def delete_files():
    select_item = list_wid.currentItem()

    if select_item is not None:
        item = list_wid.row(select_item)
        list_wid.takeItem(item)

def merger_save_files():
    try:
        file_names = all_file_paths
        pdf_finish = PyPDF2.PdfMerger()

        for name in file_names:
            pdf_finish.append(name)

        output_name, _ = QFileDialog.getSaveFileName(None, caption = 'Save PDF', filter = 'PDF Files (*.pdf)')
        pdf_finish.write(output_name)
        pdf_finish.close()

    except Exception as e:
        print(f'The error is: {e}')

# Creation of the app
app = QApplication(sys.argv)

wind = QMainWindow()
wind.setWindowTitle('Merge PDF')
wind.setGeometry(480, 250, 500, 250)

# Main Widget
main_widget = QWidget()

# Main Layout - Horizontal: This layout contains 2 vertical layouts
main_layout = QHBoxLayout(main_widget)

# First Vertical Layout
v_box_1 = QVBoxLayout()

# Button Select PDF of the First Vertical Layout
button_select_PDF = QPushButton('Select PDF files', main_widget)
button_select_PDF.setFixedSize(100, 40)
button_select_PDF.clicked.connect(select_files)

# Label and QList of the First Vertical Layout
list_lab = QLabel('List PDF files')
list_wid = QListWidget(main_widget)
list_wid.setFixedSize(240, 220)

# Add widgets to the First Vertical Layout
v_box_1.addWidget(button_select_PDF, alignment = Qt.AlignCenter)
v_box_1.addWidget(list_lab, alignment = Qt.AlignLeft)
v_box_1.addWidget(list_wid, alignment = Qt.AlignLeft)

# Second Vertical Layout
v_box_2 = QVBoxLayout()

# Button Merge and Save of the Second Vertical Layout
button_merge_save = QPushButton('Merge \nand \nSave PDF', main_widget)
button_merge_save.setFixedSize(120, 120)
button_merge_save.clicked.connect(merger_save_files)

# Button Delete of the Second Vertical Layout
button_delete = QPushButton('Delete PDF', main_widget)
button_delete.setFixedSize(70, 60)
button_delete.clicked.connect(delete_files)

# Add widgets to the Second Vertical Layout
v_box_2.addWidget(button_merge_save, alignment = Qt.AlignCenter)
v_box_2.addWidget(button_delete, alignment = Qt.AlignCenter)

# Add layouts to the Main Layout
main_layout.addLayout(v_box_1)
main_layout.addLayout(v_box_2)

# Show Main Widget
wind.setCentralWidget(main_widget)

# Show window
wind.show()

# Execute the app
sys.exit(app.exec_())

Usage flow:

  1. Select PDF files opens a native multi-select dialog. File names are displayed in the list and full paths are stored in all_file_paths.
  2. Delete PDF removes the selected file from the list (without touching the original on disk).
  3. Merge and Save PDF uses PyPDF2.PdfMerger() to concatenate the PDFs in list order, and opens a save dialog to choose destination and filename.

Why PyQt5 instead of Tkinter:

  • Native look & feel on Windows, Mac, and Linux—widgets inherit the system theme.
  • QFileDialog provides the native operating system dialog, with multi-selection, file extension filters, and standard navigation.
  • Supports signals/slots (clicked.connect(fn)), cleaner than Tkinter's command= for larger GUIs.
  • Easier to package with PyInstaller into a standalone executable—ideal for distributing to non-technical users.

Packaging as an executable:

pip install pyinstaller
pyinstaller --onefile --windowed --name "Merge PDF" merge_pdf.py

Generates a .exe (Windows) / .app (Mac) / binary (Linux) of ~60 MB that includes Python, PyQt5, and PyPDF2—the end user doesn't need to install anything.

Natural project extensions:

  • Drag & drop files directly onto the window
  • Preview the first page of each PDF
  • Reorder the list with ↑ / ↓ buttons or drag-and-drop
  • Encrypt the resulting PDF with a password (pdf_finish.encrypt('mypassword'))
  • Option to extract page ranges per file before merging

Installation:

pip install PyQt5 PyPDF2
Back to the blog