首先安裝 win32com模塊
但是在cmd中輸入pip install win32com安裝不成功。
解決辦法:輸入python -m pip install pypiwin32進行安裝,成功解決。
from win32com.client import gencache
from win32com.client import constants, gencache
import os
def createPdf(wordPath, pdfPath):
"""
word轉pdf
:param wordPath: word文件路徑
:param pdfPath: 生成pdf文件路徑
"""
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)
sourcePath="C:/Users/Administrator/Desktop/word"
files=os.listdir(sourcePath)
for file in files:
file = sourcePath+'/'+file
pdfpath=file+'.pdf'
print(pdfpath)
createPdf(file,pdfpath)
哪位大佬打包好了這個程序的話可以發我哈哈,不客氣