word doc转pdf


from win32com.client import constants, gencache

# TODO pip install pywin32 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
wordPath = r"D:Downloadgrafana+influx+jmeter.docx"
pdfPath = r"D:Downloadgrafana+influx+jmeter.pdf"


def create_pdf():
"""
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)


if __name__ == '__main__':
create_pdf()
原文地址:https://www.cnblogs.com/SunshineKimi/p/12270131.html