| 
 
	积分3625贡献 精华在线时间 小时注册时间2014-10-21最后登录1970-1-1 
 | 
 
| 
本帖最后由 15195775117 于 2021-1-31 00:42 编辑
x
登录后查看更多精彩内容~您需要 登录 才可以下载或查看,没有帐号?立即注册 
  
 工作上经常需要把文档转pdf,今天研究一下实现了,代码如下:
 #我电脑上的文档是wps_docx
 #doc转pdf需要的包:
 from win32com.client import constants,gencache
 import glob
 def createPdf(wordPath,pdfPath):
 word = gencache.EnsureDispatch('Word.Application')
 doc = word.Documents.Open(wordPath, ReadOnly=1)
 doc.ExportAsFixedFormat(pdfPath,
 constants.wdExportFormatPDF,
 Item=constants.wdExportDocumentWithMarkup,
 CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
 word.Quit(constants.wdDoNotSaveChanges)
 #我打算把以下文件夹内的doc全转为pdf:
 Path='C:\\Users\\Administrator\\Desktop\\报告集\\'
 #查找doc文件:
 docfile=glob.glob(Path+'*.docx')
 #文件数:
 n=len(docfile)
 print('doc文件数=',n)
 for i in range(0,n):
 #文件名需要用个独立的变量,docfile.split('.')非法
 name=docfile
 cut=name.split('.')
 #    注意!这里不是[0:-2],与IDL不同的是,[0:-1]不包括最后一个!
 #pdf文件名:
 pdfile='.'.join(cut[0:-1])+'.pdf'
 print(pdfile)
 #执行进度:
 print(i+1,'/',n)
 x=createPdf(docfile,pdfile)
 print('程序结束!')
 
 
 | 
 |