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:\test\test001.docx', r'D:\test\test001.pdf')

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

以上。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-08-23
猜你喜欢
  • 2022-12-23
  • 2021-10-25
  • 2021-11-18
  • 2021-08-03
  • 2021-12-21
  • 2021-12-19
  • 2022-01-25
相关资源
相似解决方案