import
PyPDF2
import
os
import
tkinter as tk
from
tkinter
import
filedialog
from
tkinter
import
messagebox
from
threading
import
Thread
def
select_output_folder():
output_folder_path
=
filedialog.askdirectory()
output_folder_entry.delete(
0
, tk.END)
output_folder_entry.insert(
0
, output_folder_path)
def
select_files_to_merge():
pdf_files
=
filedialog.askopenfilenames(filetypes
=
[(
"PDF files"
,
"*.pdf"
)])
if
pdf_files:
for
pdf_file
in
pdf_files:
pdf_listbox.insert(tk.END, pdf_file)
def
delete_selected_files():
selected_indices
=
pdf_listbox.curselection()
if
selected_indices:
for
index
in
selected_indices:
pdf_listbox.delete(index)
def
delete_all_files():
pdf_listbox.delete(
0
, tk.END)
def
merge_pdfs():
pdf_files
=
pdf_listbox.get(
0
, tk.END)
output_folder
=
output_folder_entry.get()
custom_filename
=
custom_filename_entry.get()
if
not
pdf_files:
messagebox.showerror(
"Error"
,
"没有选择要合并的PDF文件。"
)
return
def
merge_pdfs_in_thread():
output_filename
=
os.path.join(output_folder, f
"{custom_filename}.pdf"
)
pdf_merger
=
PyPDF2.PdfMerger()
for
pdf_file
in
pdf_files:
with
open
(pdf_file,
"rb"
) as pdf:
pdf_merger.append(pdf)
with
open
(output_filename,
"wb"
) as output_pdf:
pdf_merger.write(output_pdf)
result_label.config(text
=
"PDF文件已成功合并到 "
+
output_filename)
messagebox.showinfo(
"Success"
,
"PDF文件已成功合并到 "
+
output_filename)
delete_all_files()
output_folder_entry.delete(
0
, tk.END)
custom_filename_entry.delete(
0
, tk.END)
merge_thread
=
Thread(target
=
merge_pdfs_in_thread)
merge_thread.start()
def
show_instructions_window():
instructions
=
instructions_window
=
tk.Toplevel(window)
instructions_window.title(
"使用说明"
)
instructions_label
=
tk.Label(instructions_window, text
=
instructions, justify
=
"left"
)
instructions_label.pack(padx
=
10
, pady
=
10
)
window
=
tk.Tk()
window.title(
"PDF合并工具"
)
window.geometry(
"500x620"
)
instructions_button
=
tk.Button(window, text
=
"使用说明"
, command
=
show_instructions_window)
instructions_button.pack(side
=
"top"
, anchor
=
"nw"
, padx
=
10
, pady
=
10
)
select_button
=
tk.Button(window, text
=
"选择需要合并的PDF文件"
, command
=
select_files_to_merge)
select_button.pack(padx
=
10
, pady
=
10
)
frame
=
tk.Frame(window)
frame.pack(padx
=
10
, pady
=
5
, fill
=
"both"
, expand
=
True
)
pdf_listbox
=
tk.Listbox(frame, selectmode
=
tk.MULTIPLE)
pdf_listbox.pack(side
=
tk.LEFT, fill
=
tk.BOTH, expand
=
True
)
scrollbar
=
tk.Scrollbar(frame, orient
=
tk.VERTICAL, command
=
pdf_listbox.yview)
scrollbar.pack(side
=
tk.RIGHT, fill
=
tk.Y)
pdf_listbox.config(yscrollcommand
=
scrollbar.
set
)
delete_button
=
tk.Button(window, text
=
"删除选定文件"
, command
=
delete_selected_files)
delete_button.pack(side
=
"top"
, anchor
=
"nw"
, padx
=
10
, pady
=
10
)
delete_all_button
=
tk.Button(window, text
=
"删除全部文件"
, command
=
delete_all_files)
delete_all_button.pack(side
=
"top"
, anchor
=
"nw"
, padx
=
10
, pady
=
10
)
output_select_button
=
tk.Button(window, text
=
"选择合并后存放路径"
, command
=
select_output_folder)
output_select_button.pack(padx
=
10
, pady
=
10
)
output_folder_entry
=
tk.Entry(window)
output_folder_entry.pack(padx
=
10
, pady
=
5
, fill
=
"x"
)
custom_filename_label
=
tk.Label(window, text
=
"合并后的文件名:"
)
custom_filename_label.pack(padx
=
10
, pady
=
10
)
custom_filename_entry
=
tk.Entry(window)
custom_filename_entry.pack(padx
=
10
, pady
=
10
)
merge_button
=
tk.Button(window, text
=
"合并PDF"
, command
=
merge_pdfs, bg
=
"green"
, fg
=
"white"
)
merge_button.pack(side
=
"bottom"
, anchor
=
"se"
, padx
=
10
, pady
=
20
)
result_label
=
tk.Label(window, text
=
"")
result_label.pack(padx
=
10
, pady
=
10
)
window.mainloop()