创建虚拟环境
conda create -n env_pdf python=3.8
激活虚拟环境
conda activate env_pdf
安装三个库
pip install pdf2docx
pip install pysimplegui
pip install pyinstaller
import PySimpleGUI as sg
from pdf2docx import Converter
import re
# 传入文件绝对路径
def pdf_to_word(fileName):
pdf_file = fileName
#正则获取不含文件类型后缀的部分,用于组成word文档绝对路径
name = re.findall(r'(.*?)\.',pdf_file)[0]
docx_file = f'{name}.docx'
cv = Converter(pdf_file)
cv.convert(docx_file, start=0, end=None)
cv.close()
# 主题设置
sg.theme('DarkTeal7')
# 布局设置
layout = [
[sg.Text('待转换的文件是:',font=("微软雅黑", 12)),sg.Text('',key='filename',size=(50,1),font=("微软雅黑", 10),text_color='blue')],
[sg.Text('程序操作记录',justification='center')],
[sg.Output(size=(80, 20),font=("微软雅黑", 10))],
[sg.FileBrowse('选择文件',key='file',target='filename'),sg.Button('开始转换'),sg.Button('关闭程序')]
]
# 创建窗口
window = sg.Window('pdf转word工具,pdf文件名不能有中文', layout,font=("微软雅黑", 15),default_element_size=(50,1))
# 事件循环
while True:
event, values = window.read()
if event in (None, '关闭程序'):
break
if event == '开始转换':
if values['file'] and re.findall(r'\.(\S+)',values['file'])[0]=='pdf':
fileName = values['file']
pdf_to_word(fileName)
print('\n----------转换完成----------\n')
else:
print('文件未选取或文件非pdf文件\n请先选择文件')
window.close()
打包代码
pyinstaller -F -w pdf2word.py
借鉴原文链接:https://posts.careerengine.us/p/61617407be69856d63c95f22