doc转pdf, 代码如下:
from win32com import client
import os
wordhandle = client.Dispatch("Word.Application")
wordhandle.Visible = 0 # 后台运行,不显示
wordhandle.DisplayAlerts = 0 #不警告
doc = wordhandle.Documents.Open(r'D:\1.docx')
doc.SaveAs(r'D:\1.pdf', 17) # txt=4, html=10, docx=16, pdf=17
doc.Close()
wordhandle.Quit()
程序运行时,打开了要转为pdf的Word文件,出现了以下的问题:
在程序开始运行之前,关闭打开的word。
os.system('taskkill /f /im %s' % 'WINWORD.EXE')
os.system('taskkill /f /im %s' % 'wps.exe')