# author : Woo_home
# create_time : 2020/3/4 22:01
import tkinter
from tkinter import *
from tkinter.filedialog import askdirectory
import tkinter.filedialog
import tkinter.messagebox
from win32com.client import gencache
from win32com.client import constants,gencache
import os
# word 文件路径选择
def wordFile():
# 获取文件路径
file_path = tkinter.filedialog.askopenfilename()
# 将路径中的 / 替换为 \\\\
file_path.replace("/","\\\\")
# 将路径显示在 Entry 上
inPath.set(file_path)
# 转换为 pdf 的输出路径
def pdfFile():
# 目录路径选择
output_file = askdirectory()
# 在目录的基础上加上 pdf 文件的名称和后缀
output_file = output_file + '/转换.pdf'
# 将路径显示在 Entry 上
outPath.set(output_file)
# 转换成功消息
def success():
# 设置转换成功的消息提示
result = tkinter.messagebox.askokcancel(title='success',message='转换成功')
# 退出程序
def quitPorcess():
sys.exit(0)
# word 转 pdf 核心代码
def converse():
"""
word转pdf
:param inPath.get(): word文件路径
:param outPath.get(): 生成pdf文件路径
"""
word = gencache.EnsureDispatch('Word.Application')
# 打开 word 文件,ReadOnly 表示只读取一个文件
doc = word.Documents.Open(inPath.get(), ReadOnly=1)
# word 转 pdf 的主要代码实现
doc.ExportAsFixedFormat(outPath.get(),
constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
word.Quit(constants.wdDoNotSaveChanges)
# 转换成功之后调用 success 函数
success()
# GUI 代码
def GUI():
# 设置 Label 标签并且布局
Label(root,text="word 文件路径:").grid(row=1,column=0)
# 设置 Entry 输入框,textvariable 表示显示 inPath 的内容
Entry(root,textvariable=inPath).grid(row=1,column=1)
# 文件选择按钮,触发 wordFile 函数
Button(root,text="文件选择",command=wordFile).grid(row=1,column=2)
# 设置 Label 标签并且布局
Label(root,text="pdf 输出路径:").grid(row=2,column=0)
# 设置 Entry 输入框,textvariable 表示显示 outPath 的内容
Entry(root,textvariable=outPath).grid(row=2,column=1)
# 输出路径按钮,触发 pdfFile 函数
Button(root,text="输出路径",command=pdfFile).grid(row=2,column=2)
# 文件转换按钮,触发 converse 按钮
Button(root,text="Word 转 PDF",command=converse).grid(row=3,column=1)
# 程序退出按钮,触发 quitPorcess 函数
Button(root,text="退出程序",command=quitPorcess).grid(row=4,column=1)
# 显示所有组件
root.mainloop()
if __name__ == '__main__':
# 初始化
root = Tk()
# 设置标题
root.title("word 转 pdf")
# 获取输入内容
path = StringVar()
# 获取输入内容
inPath = StringVar()
# 获取输入内容
outPath = StringVar()
# 调用 GUI 函数显示界面
GUI()
没有合适的资源?快使用搜索试试~ 我知道了~
Python 实现的 word 转 pdf 小工具
共2个文件
exe:1个
py:1个
需积分: 5 5 下载量 180 浏览量
2020-03-11
15:49:23
上传
评论
收藏 11.24MB RAR 举报
温馨提示
使用 Python 语言编写,可以实现 word 文件 转 pdf 文件,使用很多的 tkinter 组件, 实现了界面可视化,操作简单方便,附代码
资源推荐
资源详情
资源评论
收起资源包目录
dist.rar (2个子文件)
word 转 pdf.exe 11.49MB
python_test.py 3KB
共 2 条
- 1
资源评论
Woo_home
- 粉丝: 1388
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功