python办公自动化(1)实现批量word转PDF


 1 from win32com.client import gencache
 2 from win32com.client import constants, gencache
 3 import os,re
 4 def createPdf(wordPath, pdfPath):
 5   """
 6   word转pdf
 7   :param wordPath: word文件路径
 8   :param pdfPath: 生成pdf文件路径
 9   """
10   word = gencache.EnsureDispatch('Word.Application')
11   doc = word.Documents.Open(wordPath, ReadOnly=1)
12   doc.ExportAsFixedFormat(pdfPath,
13               constants.wdExportFormatPDF,
14               Item=constants.wdExportDocumentWithMarkup,
15               CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
16   word.Quit(constants.wdDoNotSaveChanges)
17 ###批量转换
18 for dirs,subdirs,files in os.walk(os.getcwd()):
19     for name in files:
20         if re.search('\.(doc|docx)', name):
21             #print(dirs,subdirs,name)
22             if subdirs:
23               createPdf(dirs+subdirs+name,re.subn('(doc|docx)', 'pdf', name))
24             else:
25                createPdf(dirs+'\\'+name,dirs+'\\'+re.subn('(doc|docx)', 'pdf', name)[0])
26 
27 
28         print('--------------文档已全部转换完成-----------------------')

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM