当前位置: 首页 > news >正文

用Tkinter制作一个用于合并PDF文件的小程序

需要安装PyPDF2库,具体原代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Dec 29 14:44:20 2024@author: YBK
"""import PyPDF2
import os
import tkinter as tk 
import windndpdf_files = []
def dragged_files(files):    if len(files) > 1:for item in files:pdf_files.append(item.decode('gbk'))message.config(text='当前拖放的文件路径为:\n' + '\n'.join((file for file in pdf_files)))        else:pdf_files.append(files[0].decode('gbk'))message.config(text='当前拖放的文件路径为:\n' + '\n'.join((file for file in pdf_files))) def get_filename_without_extension(filepath):filename_with_extension = os.path.basename(filepath)filename, file_extension = os.path.splitext(filename_with_extension)return filenamedef is_pdf_file(file_path):# 使用os.path.splitext()分割文件路径和扩展名_, file_extension = os.path.splitext(file_path)# 检查扩展名是否为.pdf(注意扩展名包含点号)return file_extension.lower() == '.pdf'def on_button_click():global pdf_filesprint("生成合并的PDF文件!")output_path = os.path.dirname(pdf_files[0])outfilename = get_filename_without_extension(pdf_files[0])output_path = os.path.join(output_path,f'merged_{outfilename}.pdf')print(output_path)merge_pdfs(pdf_files, output_path)message.config(text='合成成功,当前合成PDF的文件路径为:\n' + output_path)def on_button_click0():global pdf_filespdf_files = []message.config(text='当前拖放的文件路径为:\n' + '\n'.join((file for file in pdf_files)))print("清空列表")def merge_pdfs(pdf_list, output_path):merger = PyPDF2.PdfMerger()    for pdf in pdf_list:if is_pdf_file(pdf):with open(pdf, 'rb') as f:merger.append(f)    with open(output_path, 'wb') as f:merger.write(f)if __name__ == '__main__':rootWindow = tk.Tk()rootWindow.title("按顺序拖拉PDF文件进行合并")rootWindow.geometry("400x520")message = tk.Message(rootWindow, width=400, text="当前未拖放PDF文件,请拖放文件到这里...")message.grid(row=0,columnspan=2)button0 = tk.Button(rootWindow, text="清空文件列表", command=on_button_click0)button = tk.Button(rootWindow, text="提交进行合并", command=on_button_click)# 放置按钮在主窗口上button0.grid(row=1,column=0)button.grid(row=1,column=1)windnd.hook_dropfiles(rootWindow, func=dragged_files,)rootWindow.mainloop()

运行界面:

非常实用,提高办公效率。因为现在Wps合并PDF文件需要会员,还是自己开发一个。


http://www.mrgr.cn/news/82303.html

相关文章:

  • 27.收益的定义是什么?
  • 《HTML在网络安全中的多面应用:从防范攻击到安全审查》
  • 【Rust】结构体定义域实例化
  • 【WPS】【WORDEXCEL】【VB】实现微软WORD自动更正的效果
  • 超大规模分类(三):KNN softmax
  • PHP 使用 Redis
  • linux安装git
  • 【Rust自学】9.4. 什么时候该使用panic!
  • 【Rust自学】9.3. Result枚举与可恢复的错误 Pt.2:传播错误、?运算符与链式调用
  • LeetCode算法题——螺旋矩阵ll
  • Prometheus之终极指南(The Ultimate Guide to Prometheus)
  • 题解:监控屏幕调整问题
  • UE蓝图战利品掉落动画
  • EFEVD: Enhanced Feature Extraction for Smart Contract Vulnerability Detection
  • 蓝桥杯JAVA--002
  • 基于深度学习的视觉检测小项目(五) 项目真正的开端
  • 前端如何用 canvas 做电影院选票功能
  • UE5动画蓝图
  • 24.Java 新特性扩展(重复注解、类型注解)
  • 人工智能安全与隐私——联邦遗忘学习(Federated Unlearning)
  • 51c视觉~合集40
  • 硬件设计-关于ADS54J60的校准问题
  • 多种方式访问mysql的对比分析
  • Pygame Zero(pgzrun)详解(简介、使用方法、坐标系、目录结构、语法参数、安装、实例解释)
  • NLP中的神经网络基础
  • SELECT的使用