-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFMerger.py
More file actions
103 lines (82 loc) · 3.71 KB
/
PDFMerger.py
File metadata and controls
103 lines (82 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from pypdf import PdfMerger
import os
from pathlib import Path
class PDFMergerGUI:
def __init__(self, root):
self.root = root
self.root.title("PDF Merger")
self.root.geometry("700x500")
self.pdf_files = []
# Set default output path
self.default_output = str(Path.home() / "Downloads" / "AK_merged.pdf")
# Create main frame
self.main_frame = ttk.Frame(root, padding="10")
self.main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Buttons
ttk.Button(self.main_frame, text="Add PDFs", command=self.add_pdfs).grid(row=0, column=0, pady=5)
ttk.Button(self.main_frame, text="Remove Selected", command=self.remove_pdf).grid(row=0, column=1, pady=5)
# Listbox for PDFs
self.pdf_listbox = tk.Listbox(self.main_frame, width=70, height=15)
self.pdf_listbox.grid(row=1, column=0, columnspan=2, pady=5)
# Output selection
ttk.Label(self.main_frame, text="Output File:").grid(row=2, column=0, pady=5)
self.output_var = tk.StringVar(value=self.default_output)
ttk.Entry(self.main_frame, textvariable=self.output_var, width=50).grid(row=2, column=1, pady=5)
ttk.Button(self.main_frame, text="Browse", command=self.select_output).grid(row=2, column=2, pady=5)
# Merge button
ttk.Button(self.main_frame, text="Merge PDFs", command=self.merge_pdfs).grid(row=3, column=0, columnspan=2, pady=10)
# Progress bar
self.progress = ttk.Progressbar(self.main_frame, length=400, mode='determinate')
self.progress.grid(row=4, column=0, columnspan=2, pady=5)
def add_pdfs(self):
files = filedialog.askopenfilenames(
title="Select PDF files",
filetypes=[("PDF files", "*.pdf")]
)
for file in files:
if file not in self.pdf_files:
self.pdf_files.append(file)
self.pdf_listbox.insert(tk.END, os.path.basename(file))
def remove_pdf(self):
selection = self.pdf_listbox.curselection()
if selection:
for index in reversed(selection):
self.pdf_files.pop(index)
self.pdf_listbox.delete(index)
def select_output(self):
output_file = filedialog.asksaveasfilename(
defaultextension=".pdf",
filetypes=[("PDF files", "*.pdf")],
initialfile="AK_merged.pdf",
initialdir=str(Path.home() / "Downloads")
)
if output_file:
self.output_var.set(output_file)
def merge_pdfs(self):
if not self.pdf_files:
messagebox.showerror("Error", "No PDF files selected!")
return
# Use default path if no output is selected
output_path = self.output_var.get() or self.default_output
try:
merger = PdfMerger()
total_files = len(self.pdf_files)
for i, pdf in enumerate(self.pdf_files):
merger.append(pdf)
self.progress['value'] = ((i + 1) / total_files) * 100
self.root.update_idletasks()
with open(output_path, 'wb') as f:
merger.write(f)
messagebox.showinfo("Success", "PDFs merged successfully!")
self.progress['value'] = 0
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
self.progress['value'] = 0
def main():
root = tk.Tk()
app = PDFMergerGUI(root)
root.mainloop()
if __name__ == "__main__":
main()