自制pdf合并工具
小白自制,大神勿喷,源码如下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)
# 创建选择多个PDF文件函数
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)
# 合并PDF文件函数
def merge_pdfs():
pdf_files = pdf_listbox.get(0, tk.END)# 获取选定的PDF文件列表
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 = """使用说明:
1. 选择需要合并的PDF文件,可以多次选择。
2. 选择顺序就是合并顺序,选错可以删除。
3. 选择合并后的位置和合并后的文件名称,点击合并即可。"""
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)
# 创建选择PDF文件按钮
select_button = tk.Button(window, text="选择需要合并的PDF文件", command=select_files_to_merge)
select_button.pack(padx=10, pady=10)
# 创建一个框架来容纳 PDF 文件列表框和滚动条
frame = tk.Frame(window)
frame.pack(padx=10, pady=5, fill="both", expand=True)
# 创建PDF文件列表框,放在框架内
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()
打包文件下载:https://lxiaoyu.lanzouj.com/iBKhR1f4wl5e
密码:d7y4
测试可用,文件小巧,没广告,收藏了 感谢分享
感谢分享 win7系统不能用。。。 感谢分享,很厉害 这个小工具很好用啊感谢楼主