吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5244|回复: 66
收起左侧

[Python 原创] 自制pdf合并工具

  [复制链接]
XiaoYu121 发表于 2023-11-17 18:42
小白自制,大神勿喷,源码如下

[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
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

免费评分

参与人数 16吾爱币 +18 热心值 +14 收起 理由
babysnow + 1 + 1 谢谢@Thanks!
炒酸奶 + 1 + 1 谢谢@Thanks!
A王爷 + 1 + 1 我很赞同!
digua2020 + 1 + 1 鼓励转贴优秀软件安全工具和文档!
ekobe1796 + 1 谢谢@Thanks!
Yangzaipython + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Katie + 1 我很赞同!
feixue668 + 1 我很赞同!
yanjia5286 + 1 用心讨论,共获提升!
mjst2311 + 1 + 1 谢谢@Thanks!
wangmin + 1 + 1 热心回复!
wkdxz + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
Gaara_ + 1 + 1 谢谢@Thanks!
xyg10300 + 1 + 1 我很赞同!
TuJiXiuZaYu + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
mokson 发表于 2023-11-18 08:05
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽
mokson 发表于 2023-11-18 08:01
lvzhu111 发表于 2023-11-17 23:38
TuJiXiuZaYu 发表于 2023-11-18 00:45
感谢分享
捕梦 发表于 2023-11-18 01:51

感谢分享
cat183612 发表于 2023-11-18 02:13
win7系统不能用。。。
Wisper12138 发表于 2023-11-18 02:23
感谢分享,很厉害
头像被屏蔽
lyar321 发表于 2023-11-18 05:15
提示: 作者被禁止或删除 内容自动屏蔽
xyg10300 发表于 2023-11-18 08:05
这个小工具很好用啊感谢楼主
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-8-12 01:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表