python将word文档转pdf文档

from win32com.client import gencache
from win32com.client import constants, gencache


def createPdf(wordPath, pdfPath):
	"""word转pdf
	:param wordPath: word文件路径
	:param pdfPath: 生成pdf文件路径
	"""
	word = gencache.EnsureDispatch('kwps.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__':
	createPdf(r'D:	est	est001.docx', r'D:	est	est001.pdf')

注意:上述代码只在windows平台有效。另外:我自己在测试的时候,用的是WPS,电脑里没有word office系列的,故而word = gencache.EnsureDispatch('kwps.Application') 这个位置处写的kwps,写wps不管用哦(已采坑);如果电脑中是word office 系列,则这里要注意修改成word = gencache.EnsureDispatch('Word.Application')

以上。

原文地址:https://www.cnblogs.com/lovebkj/p/13758863.html