Python (PyPDF2) Tkinter GUI Script to Merge Multiple PDF Files Desktop App Full Project For Beginners

Python (PyPDF2) Tkinter GUI Script to Merge Multiple PDF Files Desktop App Full Project For Beginners

 

Welcome folks today in this post we will be building tkinter desktop app in which we will be merging multiple pdf files using pypdf2 library. All the full source code of the application is given below.

 

 

 

Get Started

 

 

In order to get started we need to install the following library using the pip command as shown below

 

pip install tkinter

 

pip install pypdf2

 

After installing all the libraries you need to make an app.py file and copy paste the following code

 

app.py

 

import tkinter as tk
from tkinter.filedialog import askopenfilename
from PyPDF2 import PdfFileMerger, PdfFileReader
from pathlib import Path

filelist = []

# initiate merger Object
merger = PdfFileMerger()

def open_file(files):
    filepath = askopenfilename(
        filetypes=[("PDF Files","*.pdf"), ("All Files", "*.*")]
    )
    if not(filepath and Path(filepath).exists()):
        return
    files.append(filepath)
    # list out all filenames
    lbl_items["text"] = '\n'.join(str(f) for f in files)
    if len(files) >= 2 and btn_merge['state'] == "disabled":
        btn_merge["state"] = "normal"

def merge_pdfs(files):
    for f in files:
        merger.append(PdfFileReader(open(f, "rb")))
    
    output_filename = ent_output_name.get()

    if not output_filename:
        output_filename = "Untitled.pdf"
    elif ".pdf" not in output_filename:
        output_filename += ".pdf"
    merger.write(output_filename)

# create desktop GUI
window = tk.Tk()
window.title("PDFMerger Tk")
window.geometry("500x500")
# not allowed resizing x y direction
window.resizable(0,0)

# --- Ask open files ---
fr_bg1 = tk.Frame(window, bd=3)
lbl_open = tk.Label(fr_bg1, text="Please choose PDFs to join: (2 and above)")
lbl_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)

btn_open = tk.Button(fr_bg1, text="Open file(s)",
                command=lambda: open_file(filelist))
btn_open.grid(row=1, column=0, sticky="ew", padx=5)
lbl_items = tk.Label(fr_bg1, text="")
lbl_items.grid(row=2, column=0, pady=5)
fr_bg1.pack()

# --- Button to merge PDFs ---
fr_bg2 = tk.Frame(window, bd=3)
lbl_to_merge = tk.Label(fr_bg2, text="Merge selected files (in PDF)")
lbl_to_merge.grid(row=0, column=0, sticky="ew", padx="5", pady="5")

ent_output_name = tk.Entry(master=fr_bg2, width=7)
ent_output_name.grid(row=1, column=0, sticky="ew")

btn_merge = tk.Button(fr_bg2, 
                text="Merge PDF",
                state="disabled",
                command=lambda: merge_pdfs(filelist))
btn_merge.grid(row=2, column=0, sticky="ew", padx=5, pady=5)
fr_bg2.pack()

# --- Button to exit ---
btn_exit = tk.Button(window, text="Exit", command=window.destroy, bd=2)
btn_exit.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=tk.FALSE)

if __name__ == "__main__":
    window.mainloop()

 

See also  Python 3 (Tabula) Example Script to Extract All Tables From PDF Document and Convert to XLSX and CSV Files Full Project For Beginners

 

And now if you execute the python app by typing following command

 

python app.py

 

 

 

 

 

 

Leave a Reply