公司动态
python:word convert pdf
# encoding: utf-8 # 版权所有 2023 ©涂聚文有限公司 # 许可信息查看 word covert pdf # 描述pip install pywin32 # # pip install PyPDF2 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # Datetime : 2023/10/26 14:51 # User : geovindu # Product : PyCharm # Project : pythonWebScreenShot # File : WordCovertPdf.py # explain : 学习 import os # 导入系统功能模块 from win32com.client import Dispatch, DispatchEx # 导入win32com模块的client包下的函数 from win32com.client import constants # 导入win32com模块的client包下的保存COM常量的类 from win32com.client import gencache # 导入win32com模块的client包下的gencache函数 import re # 导入正则表达式模块 import pythoncom # 导入封装了OLE自动化API的模块该模块为win32com的子模块 class WordTPdf(object): WORD转成PDF def __init__(self): self.dic self.filename def wordtopdf(self,filelist, targetpath): WORD转成PDF :param filelist: word 列表 :param targetpath: pdf 列表 :return: valueList [] try: # 调用线程初始化COM库解决调用Word 2007时出现“尚未调用CoInitialize”错误的问题 pythoncom.CoInitialize() gencache.EnsureModule({00020905-0000-0000-C000-000000000046}, 0, 8, 4) # 开始转换 w Dispatch(Word.Application) for fullfilename in filelist: (filepath, filename) os.path.split(fullfilename) # 分割文件路径和文件名其中filepath表示文件路径filename表示文件名 softfilename os.path.splitext(filename) # 分割文件名和扩展名 os.chdir(filepath) doc os.path.abspath(filename) os.chdir(targetpath) pdfname softfilename[0] .pdf output os.path.abspath(pdfname) pdf_name output # 文档路径需要为绝对路径因为Word启动后当前路径不是调用脚本时的当前路径。 try: # 捕捉异常 doc w.Documents.Open(doc, ReadOnly1) doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,\ Itemconstants.wdExportDocumentWithMarkup, CreateBookmarksconstants.wdExportCreateHeadingBookmarks) except Exception as e: # 处理异常 print(e) if os.path.isfile(pdf_name): # 判断文件是否存在 valueList.append(pdf_name) # 添加到文件列表中 else: print(转换失败) return False w.Quit(constants.wdDoNotSaveChanges) # 退出Word应用程序 return valueList # 返回生成PDF文件列表 except TypeError as e: print(出错了) print(e) return False def getfilenames(self,filepath, filelist_out[], file_extall): :param filepath: 要转换的文件路径 :param filelist_out: 转换成PDF的列表 :param file_ext: 扩展名 :return: 返回转换成PDF的列表 # 遍历filepath下的所有文件包括子目录下的文件 #print(filepath) #print(filelist_out) for fpath, dirs, fs in os.walk(filepath): for f in fs: fi_d os.path.join(fpath, f) if file_ext .doc: # 遍历Word文档文件 if os.path.splitext(fi_d)[1] in [.doc, .docx]: # 判断是否为Word文件 filelist_out.append(re.sub(r\\, /, fi_d)) # 添加到路径列表中 else: if file_ext all: # 要获取所有文件的情况 #print(fi_d) filelist_out.append(fi_d) # 将文件路径添加到路径列表中 elif os.path.splitext(fi_d)[1] file_ext: # 要获取除了Word文件以外的文件 filelist_out.append(fi_d) # 将文件路径添加到路径列表中 else: pass filelist_out.sort() # 对路径进行排序 #print(filelist_out) return filelist_out # 返回文件完整路径列表介绍了一个将Word文档转换为PDF的Python工具类WordTPdf主要功能包括使用pywin32库调用Microsoft Word应用程序实现文档转换支持批量转换指定目录下的所有Word文档.doc和.docx格式提供文件路径遍历功能可获取指定扩展名的文件列表转换过程处理了COM库初始化、异常捕获等细节转换后的PDF会保留原文档的标记和标题书签核心方法wordtopdf()执行Word到PDF的转换getfilenames()遍历获取指定路径下的文件列表该工具需要安装pywin32库适用于需要批量处理Word转PDF的场景转换结果保存到指定目录。