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:\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')

以上。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM