首页 > 编程语言 >用Python制作一个PDF转Word工具(V2.1)

用Python制作一个PDF转Word工具(V2.1)

时间:2023-08-24 13:56:24浏览次数:64  
标签:Word tkinter Python V2.1 window place pdf path anchor

用Python制作一个PDF转Word工具
工具:Python3.9.13,VSCode1.73.1,pdf2docx0.5.6,tkinter,Win10Home
PDF文件不易编辑,想要编辑需要转成Word,但网上的工具很多要充VIP,所以今天我们就来做个PDF转Word工具。
首先先安装第三方库:

```bash
pip install tkinter
```

导入库:

```python
#coding=utf-8
import os
import tkinter
from pdf2docx import parse
from tkinter import filedialog
from tkinter.filedialog import askdirectory
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import *
```

调用第三方库进行转换和选择文件操作:

```python
pdf_path = ""
_path_ = ""
def selectPath():
global _path_
_path_ = askdirectory()
_path_ = _path_.replace("/", "\\")
temp2.set(_path_)
entry2.configure(textvariable = temp2)
def transPath():
_path_ = str(entry2.get())
for filename in os.listdir(_path_):
if (filename.endswith(".pdf")):
global pdf, docx
pdf = os.path.join(_path_, filename)
docx = pdf[:-4] + '.docx'
parse(pdf, docx)
showinfo("提示", "转换完成,文件保存在原目录!")
def trans():
pdf_file = str(entry1.get())
docx_file = pdf_file[:-4] + '.docx'
parse(pdf_file, docx_file)
showinfo("提示", "转换完成,文件保存在原目录!")
def getPath():
temp = tkinter.Tk()
temp.withdraw()
global pdf_path
pdf_path = filedialog.askopenfilename(title='选择PDF', filetypes=[('PDF Files', '*.pdf'), ('All Files', '*.')])
temp1.set(pdf_path)
entry1.configure(textvariable = temp1)
```

最后再搭建GUI界面:

```python
window = tkinter.Tk()
window.title('PDF转Word工具2.1 Powered by 印皓显')
window.geometry('700x450')
# window.resizable (0, 0)
text1 = 'PDF转Word工具'
lb = tkinter.Label(window, text = text1, width=13, height = 1, justify = 'center', anchor = 'nw', font = ('宋体',18), fg = 'white', bg = 'grey', padx = 10, pady = 5)
lb.place(x = 350, y = 25, anchor = "center")
current_work_dir = os.path.dirname(__file__)
img_png = PhotoImage(file = current_work_dir + '\\Pdf转word_clear_compress - 副本.gif')
label_img = Label(window, image = img_png)
label_img.place(x=180, y=50, anchor='n')
temp1 = StringVar()
entry1 = ttk.Entry(window, textvariable = temp1, width = 36)
entry1.place (x = 375, y = 100, anchor = "nw")
Button1 = ttk.Button(window,text="选择文件",command = getPath)
Button1.place(x = 375, y = 145, anchor = "nw")
Button2 = ttk.Button(window,text="一键转换",command = trans)
Button2.place(x=550, y=145, anchor="nw")
temp2 = StringVar()
entry2 = ttk.Entry(window, textvariable = temp2, width = 36)
entry2.place (x = 375, y = 300, anchor = "nw")
Button3 = ttk.Button(window,text="选择目录",command = selectPath)
Button3.place(x = 375, y = 350, anchor = "nw")
Button4 = ttk.Button(window,text="批量转换",command = transPath)
Button4.place(x = 550, y = 350, anchor = "nw")
window.mainloop()
```

 

所有代码:

```python
#coding=utf-8
import os
import tkinter
from pdf2docx import parse
from tkinter import filedialog
from tkinter.filedialog import askdirectory
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import *
pdf_path = ""
_path_ = ""
def selectPath():
global _path_
_path_ = askdirectory()
_path_ = _path_.replace("/", "\\")
temp2.set(_path_)
entry2.configure(textvariable = temp2)
def transPath():
_path_ = str(entry2.get())
for filename in os.listdir(_path_):
if (filename.endswith(".pdf")):
global pdf, docx
pdf = os.path.join(_path_, filename)
docx = pdf[:-4] + '.docx'
parse(pdf, docx)
showinfo("提示", "转换完成,文件保存在原目录!")
def trans():
pdf_file = str(entry1.get())
docx_file = pdf_file[:-4] + '.docx'
parse(pdf_file, docx_file)
showinfo("提示", "转换完成,文件保存在原目录!")
def getPath():
temp = tkinter.Tk()
temp.withdraw()
global pdf_path
pdf_path = filedialog.askopenfilename(title='选择PDF', filetypes=[('PDF Files', '*.pdf'), ('All Files', '*.')])
temp1.set(pdf_path)
entry1.configure(textvariable = temp1)
window = tkinter.Tk()
window.title('PDF转Word工具2.1 Powered by 印皓显')
window.geometry('700x450')
# window.resizable (0, 0)
text1 = 'PDF转Word工具'
lb = tkinter.Label(window, text = text1, width=13, height = 1, justify = 'center', anchor = 'nw', font = ('宋体',18), fg = 'white', bg = 'grey', padx = 10, pady = 5)
lb.place(x = 350, y = 25, anchor = "center")
current_work_dir = os.path.dirname(__file__)
img_png = PhotoImage(file = current_work_dir + '\\Pdf转word_clear_compress - 副本.gif')
label_img = Label(window, image = img_png)
label_img.place(x=180, y=50, anchor='n')
temp1 = StringVar()
entry1 = ttk.Entry(window, textvariable = temp1, width = 36)
entry1.place (x = 375, y = 100, anchor = "nw")
Button1 = ttk.Button(window,text="选择文件",command = getPath)
Button1.place(x = 375, y = 145, anchor = "nw")
Button2 = ttk.Button(window,text="一键转换",command = trans)
Button2.place(x=550, y=145, anchor="nw")
temp2 = StringVar()
entry2 = ttk.Entry(window, textvariable = temp2, width = 36)
entry2.place (x = 375, y = 300, anchor = "nw")
Button3 = ttk.Button(window,text="选择目录",command = selectPath)
Button3.place(x = 375, y = 350, anchor = "nw")
Button4 = ttk.Button(window,text="批量转换",command = transPath)
Button4.place(x = 550, y = 350, anchor = "nw")
window.mainloop()
```
![image](https://s1.ax1x.com/2023/07/14/pC4UENV.png))
最后用auto-py-to-exe打包成exe,再用Inno Setup打包成安装包。(~~无聊~~)
安装包下载
安装包:
链接:https://pan.baidu.com/s/1CtOdB9zJKKr2uCVKZtXtgA
提取码:1234
绿色版:
链接:https://pan.baidu.com/s/1pti8YRlGIrJQHUDbNqBQPQ
提取码:1234
(图片获取也可以在绿色版目录下找)

标签:Word,tkinter,Python,V2.1,window,place,pdf,path,anchor
From: https://www.cnblogs.com/ZnO-YHX/p/17653954.html

相关文章

  • Python高速下载 、git高速下载、PyCharm高速下载
    githttps://pc.qq.com/detail/13/detail_22693.htmlhttps://webcdn.m.qq.com/spcmgr/download/Git-2.42.0-64-bit.exePyCharmhttps://pc.qq.com/detail/12/detail_26692.htmlhttps://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows&code......
  • centos 7上,如何升级python到最新的版本?
    在centos7上yum安装出来的,python版本是3.6.8[root@centos7~]#python3--versionPython3.6.8  想要升级到最新的版本,如何操作? 本文介绍通过源码的方式,升级python到最新的版本。 1、下载python最新的源码包 登录下面的地址:https://www.python.org/downloads......
  • Python相关
    python-内置函数内置函数是python预先定义的函数,这些函数可以提高我们的编码效率。常用的比如map,他可以传两个参数,分别是函数和指定的序列,会根据你提供的函数对指定的序列做映射,还比如zip,我们也叫拉链,就是把两个可迭代的对象打包成一个个元组。还有像hash,是获取一个对象的hash值,e......
  • Python-PyMySQL的一些使用注意事项
    一、关于groupby的使用在部分mysql版本(5.7.xx及以上)中,若select的列中,包含了未被groupby的字段,会报以下错误:[Err]1055-Expression#1ofORDERBYclauseisnotinGROUPBYclauseandcontainsnonaggregatedcolumn'xxxx'whichisnotfunctionallydependentoncolu......
  • springBoot 整合 poi 导出带有复杂表格(合并表格)的word文件
    1.Maven依赖见上一篇文章直接贴源码如下:packagecom.mingx.pms.web.system.file;importcn.hutool.core.date.DateUtil;importcom.mingx.pms.constant.SystemInfo;importcom.mingx.pms.entities.workplan.plan.vo.WorkPlanDetailExportVO;importcom.mingx.pms.entities......
  • Python Web:Django、Flask和FastAPI框架对比
    Django、Flask和FastAPI是PythonWeb框架中的三个主要代表。这些框架都有着各自的优点和缺点,适合不同类型和规模的应用程序。Django:Django是一个全功能的Web框架,它提供了很多内置的应用程序和工具,使得开发Web应用程序更加容易。Django采用了MTV(模型-模板-视图)设计模式,提供ORM......
  • Python-大文件上传requests-toolbelt
    一、简介requests.post发送文件的方式是把所有文件读取内存中,再构建请求发送出去。当发送大文件时候(好几个G)就会导致内存不足OOM默认使用的requests.post较难做到分段文件发送(大概思路:先计算原文件md5,再切分文件发送,到接收端组合再计算md5,较麻烦不推荐,可参考:Python之requests模......
  • Python-保存request请求为各种文件
    文件下载相关工具importjsonimportrequestsclassCustomFileTools(object):defdownload_json_file(self,json_url,save_path):"""下载json文件并保存"""json_req=requests.get(json_url)ifjso......
  • python中的 “__name__”属性
       在python中,每个脚本文件都会有自己的__name__属性。在单独运行一个脚本时,name属性会被赋值为__main__,这并不是说当前脚本名字叫main,这个值表示当前脚本是主函数。      如果现在有一个test1脚本和test2脚本。单独运行test1脚本时,这个属性就会被赋......
  • Python-Flask配置https证书
    零、问题错误:SSLVersion2and3ProtocolDetection一、说明1、通过使用flask框架配置https证书进行HTTPs证书配置后会被扫描到漏洞,漏洞描述如下:远程服务接受使用SSL2.0和/或SSL3.0加密的连接。这些版本的SSL受到多个加密缺陷的影响,包括:-带CBCCiphers的不安全的填充方案。......