python学习源码分享:Word文档批量转换为PDF

【python学习源码分享:Word文档批量转换为PDF】# -*- coding:utf-8 -*-import os from win32com.client import Dispatch, DispatchEx from win32com.client import constantsfrom win32com.client import gencacheimport redef getfilenames(filepath='',filelist_out=[],file_ext='all'):for fpath, dirs, fs in os.walk(filepath):for f in fs:fi_d = os.path.join(fpath, f)if file_ext == '.doc':if os.path.splitext(fi_d)[1] in ['.doc','.docx']:filelist_out.append(re.sub(r'\','/',fi_d))else:iffile_ext == 'all':filelist_out.append(fi_d)elif os.path.splitext(fi_d)[1] == file_ext:filelist_out.append(fi_d)else:passfilelist_out.sort()return filelist_outdef wordtopdf(filelist,targetpath,digit):valueList = []try:gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)# 开始转换w = Dispatch("Word.Application")for index,fullfilename in enumerate(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 = str(index).zfill(digit)".pdf"output = os.path.abspath(pdfname)pdf_name = output# 文档路径需要为绝对路径 , 因为Word启动后当前路径不是调用脚本时的当前路径 。try:doc = w.Documents.Open(doc, ReadOnly=1)doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,Item=constants.wdExportDocumentWithMarkup,CreateBookmarks=constants.wdExportCreateHeadingBookmarks)except Exception as e:print(e)if os.path.isfile(pdf_name):valueList.append(pdf_name)else:print('转换失败!')return Falsew.Quit(constants.wdDoNotSaveChanges)return valueListexcept TypeError as e:print('出错了!')print(e)return Falseif __name__ == '__main__':sourcepath = r"E:/learn/test/doc/temp"targetpath = r"E:/learn/test/doc/pdf/"filelist = getfilenames(sourcepath,[],'.doc')valueList = wordtopdf(filelist,targetpath,4)if valueList:print("转换成功")else:print("没有要转换的Word文档或者转换失败!")

相关经验推荐