pdf转word程序2.0
准备工作
所需要的库:
ttkbootstrap
安装:
pip install ttkbootstrap
pdf2docx
安装:
pip install pdf2docx
docx2pdf
安装:
pip install docx2pdf
PS:此程序不能用pyinstaller直接打包
应在打包时加上
--collect-all ttkbootstrap
比如
pyinstaller test.py -F -w --collect-all ttkbootstrap
更新内容
1.更新 GUI
全新的 GUI 使用了 ttkbootstrap 库
这是一个基于 tkinter 的库
使用起来和 tkinter.ttk 基本相同
官方文档:https://ttkbootstrap.readthedocs.io/en/latest/ (较慢)
2.更新了单文件pdf转word的保存位置
现在转换成的word文档
会保存在源文件夹里
对比:
v1.0版本的代码:
user_name = os.getlogin() # 获取你的用户名 desktop_path = f'C:/Users/{user_name}/Desktop/test.docx' # docx文件路径,默认设置在桌面
v2.0版本的代码:
wordfile = os.path.splitext(pdffile)[0] + '.docx'
3.更新了word转pdf
使用了docx2pdf库来实现word转pdf
源代码
import tkinter.filedialog as tf import ttkbootstrap as ttk from ttkbootstrap.constants import * from ttkbootstrap.dialogs import Messagebox as msgbox from pdf2docx import Converter from docx2pdf import convert import os def pdf_to_words(): folder = tf.askdirectory(title='选择文件夹') try: for file in os.listdir(folder): suff_name = os.path.splitext(file)[1] # 分离出扩展名 if suff_name != '.pdf': # 如果不是PDF文档就退出 continue file_name = os.path.splitext(file)[0] # 分离出文件名 pdf_file = folder + '\\' + file word_file = folder + '\\' + file_name + '.docx' # 进行转义并加上扩展名 cv = Converter(pdf_file) # 转成Word cv.convert(word_file) cv.close() msgbox.show_info('转换成功!') except FileNotFoundError: msgbox.show_error('你没有选择任何文件夹!') def pdf_to_word(): try: pdffile = tf.askopenfilename(title='选择PDF文档') # 选择文件 wordfile = os.path.splitext(pdffile)[0] + '.docx' cv = Converter(pdffile) cv.convert(wordfile) cv.close() msgbox.show_info('转换成功!') except FileNotFoundError: msgbox.show_error('你没有选择任何文件!') def word_to_pdf(): try: inputfile = tf.askopenfilename(title='选择docx文档') outputfile = os.path.splitext(inputfile)[0] + '.pdf' f1 = open(outputfile, 'w') f1.close() convert(inputfile, outputfile) msgbox.show_info('转换成功!') except FileNotFoundError: msgbox.show_error('你没有选择任何文件!') def about(): msgbox.show_info(message='版本号:v2.0\n制作者:鱼C论坛 liuhongrun2022') root = ttk.Window() root.title('PDF_to_Word2.0') root.geometry('900x100') b1 = ttk.Button(root, text="pdf批量转word", bootstyle='info-outline', command=pdf_to_words) b1.pack(side=LEFT, padx=5, pady=5, fill=BOTH) b2 = ttk.Button(root, text='pdf单文件转word', bootstyle='warning-outline', command=pdf_to_word) b2.pack(side=LEFT, padx=5, pady=5, fill=BOTH) b5 = ttk.Button(root, text='word转pdf',bootstyle='info-outline', command=word_to_pdf) b5.pack(side=LEFT, padx=5, pady=5, fill=BOTH) b3 = ttk.Button(root, text='关于', bootstyle='success-outline', command=about) b3.pack(side=LEFT, padx=5, pady=5, fill=BOTH) b4 = ttk.Button(root, text='退出',bootstyle='danger-outline', command=exit) b4.pack(side=LEFT, padx=5, pady=5, fill=BOTH) l = ttk.Label(root, bootstyle='inverse-warning', text='转换中出现"未响应"为正常现象。') l.pack(side=LEFT, padx=5, pady=5, fill=BOTH) l.configure(font=('微软雅黑 Light', 9)) root.mainloop()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!