-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicMergeV2.py
More file actions
187 lines (140 loc) · 6.34 KB
/
MagicMergeV2.py
File metadata and controls
187 lines (140 loc) · 6.34 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python3
'''
Magic Merge by Alayna Ferdarko
Originally created 21 February, 2025.
Updated on 24 March, 2025.
Version 2.1 Overview:
Added ability to split PDFs.
'''
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import os
import fitz # PyMuPDF
# --- Functions ---
def merge_pdfs():
input_files = file_list.get(0, tk.END)
if not input_files:
messagebox.showerror("Error", "No PDFs selected for merging.")
return
output_path = save_location_entry.get().strip()
if not output_path:
messagebox.showerror("Error", "Select a save location for the merged PDF.")
return
progress.start()
try:
merged_pdf = fitz.open()
for file in input_files:
with fitz.open(file) as pdf:
merged_pdf.insert_pdf(pdf)
merged_pdf.save(output_path)
merged_pdf.close()
messagebox.showinfo("Success", f"PDFs merged successfully!\nSaved as: {output_path}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred:\n{e}")
progress.stop()
def browse_files():
files = filedialog.askopenfilenames(filetypes=[("PDF Files", "*.pdf")])
if files:
for file in files:
file_list.insert(tk.END, file)
def remove_selected():
selected_files = file_list.curselection()
for index in reversed(selected_files):
file_list.delete(index)
def browse_save_location():
save_path = filedialog.asksaveasfilename(defaultextension=".pdf",
filetypes=[("PDF Files", "*.pdf")],
title="Save Merged PDF As")
if save_path:
save_location_entry.delete(0, tk.END)
save_location_entry.insert(0, save_path)
def split_pdf():
input_path = split_file_path.get().strip()
page_range_str = range_entry.get().strip()
output_path = split_save_location_entry.get().strip()
if not input_path or not os.path.isfile(input_path):
messagebox.showerror("Error", "Select a valid PDF to split.")
return
if not page_range_str:
messagebox.showerror("Error", "Enter a page range.")
return
if not output_path:
messagebox.showerror("Error", "Select a save location for the split PDF.")
return
split_progress.start()
try:
with fitz.open(input_path) as doc:
total_pages = doc.page_count
# Parse only one range for simplicity: start-end
if '-' not in page_range_str:
messagebox.showerror("Error", "Enter a range in the format 'start-end'.")
split_progress.stop()
return
start_str, end_str = page_range_str.split('-')
start_page = int(start_str.strip()) - 1 # Convert to 0-indexed
end_page = int(end_str.strip()) - 1
if start_page < 0 or end_page >= total_pages or start_page > end_page:
split_progress.stop()
messagebox.showerror("Error", "Page range is out of bounds.")
return
new_doc = fitz.open()
new_doc.insert_pdf(doc, from_page=start_page, to_page=end_page)
new_doc.save(output_path)
new_doc.close()
messagebox.showinfo("Success", f"Split PDF saved as:\n{output_path}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred:\n{e}")
split_progress.stop()
def browse_split_file():
file = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
if file:
split_file_path.set(file)
def browse_split_save_location():
file_path = filedialog.asksaveasfilename(defaultextension=".pdf",
filetypes=[("PDF Files", "*.pdf")],
title="Save Split PDF As")
if file_path:
split_save_location_entry.delete(0, tk.END)
split_save_location_entry.insert(0, file_path)
# --- UI Setup ---
app = tk.Tk()
app.title("Magic Merge v.2 by Alayna Ferdarko")
app.geometry("600x700")
# --- Merge Section ---
merge_frame = tk.LabelFrame(app, text="Merge PDFs", padx=10, pady=10)
merge_frame.pack(pady=10)
file_list = tk.Listbox(merge_frame, width=70, height=8)
file_list.grid(row=0, column=0, columnspan=3, pady=5)
browse_button = tk.Button(merge_frame, text="Add PDFs", command=browse_files)
browse_button.grid(row=1, column=0, pady=5)
remove_button = tk.Button(merge_frame, text="Remove Selected", command=remove_selected)
remove_button.grid(row=1, column=1, pady=5)
save_location_entry = tk.Entry(merge_frame, width=50)
save_location_entry.grid(row=2, column=0, pady=5)
save_browse_button = tk.Button(merge_frame, text="Browse Save Location", command=browse_save_location)
save_browse_button.grid(row=2, column=1, pady=5)
progress = ttk.Progressbar(merge_frame, mode="indeterminate")
progress.grid(row=3, column=0, columnspan=3, pady=5)
merge_button = tk.Button(merge_frame, text="Merge PDFs", command=merge_pdfs)
merge_button.grid(row=4, column=0, columnspan=3, pady=5)
# --- Split Section ---
split_frame = tk.LabelFrame(app, text="Split PDF", padx=10, pady=10)
split_frame.pack(pady=10)
split_file_path = tk.StringVar()
split_file_button = tk.Button(split_frame, text="Select PDF to Split", command=browse_split_file)
split_file_button.grid(row=0, column=0, pady=5)
split_file_label = tk.Label(split_frame, textvariable=split_file_path, wraplength=400)
split_file_label.grid(row=1, column=0, pady=5)
range_label = tk.Label(split_frame, text="Enter Page Range (e.g., 1-3,5,7-9):")
range_label.grid(row=2, column=0, pady=5)
range_entry = tk.Entry(split_frame, width=50)
range_entry.grid(row=3, column=0, pady=5)
split_save_location_entry = tk.Entry(split_frame, width=50)
split_save_location_entry.grid(row=4, column=0, pady=5)
split_save_button = tk.Button(split_frame, text="Browse Save Location", command=browse_split_save_location)
split_save_button.grid(row=5, column=0, pady=5)
split_progress = ttk.Progressbar(split_frame, mode="indeterminate")
split_progress.grid(row=6, column=0, pady=5)
split_button = tk.Button(split_frame, text="Split PDF", command=lambda: split_pdf())
split_button.grid(row=7, column=0, pady=5)
app.mainloop()