首页 > 编程语言 >用Python制作一个PDF转Word工具

用Python制作一个PDF转Word工具

时间:2023-08-26 10:33:55浏览次数:38  
标签:Word tkinter Python import window pdf PDF path anchor

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

pip install tkinter

导入库:

#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)

最后再搭建GUI界面:

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()

所有代码:

#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)
最后用auto-py-to-exe打包成exe,再用Inno Setup打包成安装包。(无聊
安装包下载
安装包:
链接:https://pan.baidu.com/s/1CtOdB9zJKKr2uCVKZtXtgA
提取码:1234
绿色版:
链接:https://pan.baidu.com/s/1pti8YRlGIrJQHUDbNqBQPQ
提取码:1234
(图片获取也可以在绿色版目录下找)

标签:Word,tkinter,Python,import,window,pdf,PDF,path,anchor
From: https://www.cnblogs.com/ZnO-YHX/p/17658450.html

相关文章

  • 【873】Python读取NetCDF中的scale_factor和add_offset
    参考:python中scale的用法_在netCDF4和Python中使用scale_factor和add_offset的示例?参考代码:importnetCDF4asncdir_path="./2m_temperature/03_TIFF/"files=os.listdir(dir_path)files=sorted(files)forfileinfiles:iffile.find('.tiff')<......
  • Python基础教程06 - 循环
    循环用于重复执行一些程序块。从上一讲的选择结构,我们已经看到了如何用缩进来表示程序块的隶属关系。循环也会用到类似的写法。for循环for循环需要预先设定好循环的次数(n),然后执行隶属于for的语句n次。基本构造是for元素in序列:   statement举例来说,我们编辑一个叫forDemo......
  • 无涯教程-Python - 正则表达示
    正则表达式是特殊的字符序列,可使用模式中保留的特殊语法来帮助您匹配或查找其他字符串或字符串集。Python模块re提供对Python中类似Perl的正则表达式的全面支持。如果在编译或使用正则表达式时发生错误,则re模块会引发异常re.error。Match函数此函数尝试使用可选的标志将RE......
  • word_wjuan
    importrandomimporttimefromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServicefromselenium.webdriver.common.proxyimportProxy,ProxyTypefromselenium.webdriver.common.byimportByfromselenium.webdriver.common.action_chains......
  • 分享一个批量转换某个目录下的所有ppt->pdf的Python代码
    大家好,我是皮皮。一、前言前几天在Python最强王者群【Python小小小白】分享了一份Python自动化办公的代码,可以批量转换某个目录下的所有ppt->pdf,非常强大。二、实现过程在正式跑代码之后,你可能需要按照对应的库,不然会报错。代码运行之后,本地会出现下面的UI界面,选择PPT文件......
  • 学会Python Requests库+Cookies模拟自动登录!
    importrequestsurl="https://my.cheshi.com/user/"headers={"User-Agent":"Mozilla/5.0(Macintosh;IntelMacOSX10_15_7)AppleWebKit/537.36(KHTML,likeGecko)Chrome/116.0.0.0Safari/537.36"}res=requests.get(......
  • python网络编程
    1.套接字套接字(Socket)是实现网络编程进行数据传输的一种技术手段,网络上各种各样的网络服务大多都是基于Socket来完成通信的。socket是传输层提供给应用层的编程接口。所以,套接字socket编程分为TCP与UDP两类。在python中,通过Python套接字编程模块:importsocket提供socket......
  • Python 运算符优先级
    Python运算符优先级所谓优先级,就是当多个运算符同时出现在一个表达式中时,先执行哪个运算符。例如对于表达式a+b*c,Python会先计算乘法再计算加法;b*c的结果为8,a+8的结果为24,所以d最终的值也是24。先计算*再计算+,说明*的优先级高于+。Python支持几十种运算符,被划分......
  • 【Python-每日技巧】列举一些Python稍微有点难度的技巧
    元编程(Metaprogramming):这是一项高级技术,允许你在运行时动态地创建、修改和操作代码。Python提供了强大的元编程特性,如使用装饰器(Decorators)、元类(Metaclasses)和反射(Reflection)等。这些概念需要深入理解Python的对象模型和元数据处理能力。以下是一个使用元编程的示例,展示如何动态......
  • 无涯教程-Python - 类/对象
    如果您以前没有使用面向对象(OO)编程的经验,则可能需要查阅有关它的入门课程或至少某种形式的教程,以便掌握基本概念。创建类class语句创建一个新的类定义。该类的名称紧随关键字class后跟冒号,如下所示-classClassName:'Optionalclassdocumentationstring'class......