# -*-coding:utf-8 -*- import os import tkinter as tk from tkinter import filedialog import comtypes.client from docx import Document def browse_folder(): folder_path = filedialog.askdirectory() folder_path = folder_path.replace('/', '\\') return folder_path def browse_file(): file_path = filedialog.askopenfilenames() return file_path def convert_to_pdf(file_path, save_path): # 读取Word文档 doc = Document(file_path) # 创建一个Word应用程序对象 word = comtypes.client.CreateObject('Word.Application') # 打开Word文档 doc_object = word.Documents.Open(file_path) # 将文档另存为PDF pdf_path = os.path.join(save_path, os.path.splitext(os.path.basename(file_path))[0] + '.pdf') doc_object.SaveAs(pdf_path, FileFormat=17) # 关闭Word文档和应用程序 doc_object.Close() word.Quit() print(f"Converted {file_path} to {pdf_path}") def main(): root = tk.Tk() root.title("批量Word转PDF") folder_path = tk.StringVar() file_path = tk.StringVar() save_path = tk.StringVar() tk.Label(root, text="Word文件夹路径:").grid(row=0, column=0) tk.Entry(root, textvariable=folder_path).grid(row=0, column=1) tk.Button(root, text="浏览文件夹", command=lambda: folder_path.set(browse_folder())).grid(row=0, column=2) tk.Label(root, text="PDF保存路径:").grid(row=1, column=0) tk.Entry(root, textvariable=save_path).grid(row=1, column=1) tk.Button(root, text="浏览文件", command=lambda: save_path.set(browse_folder())).grid(row=1, column=2) tk.Button(root, text="确定", command=lambda: convert_to_pdf(file_path.get(), save_path.get())).grid(row=2, column=0) tk.Button(root, text="清空", command=lambda: (file_path.set(""), save_path.set("")).grid(row=2, column=1)) # tk.Progressbar(root, orient=tk.HORIZONTAL, length=500, mode='determinate').grid(row=3, column=0, columnspan=2) root.mainloop() if __name__ == "__main__": main()
标签:word,批量,tk,file,path,pdf,folder,root,row From: https://www.cnblogs.com/flyingsir/p/17851839.html