python中的win32com库是什么?
python中的win32com库是什么?
我们在日常生活中有时候直接生成PDF比较困难,可以换个思路,先生成Word文档,再使用win32com库将Word文档转为PDF文档,这样的转换基本上100%保留了Word的样式。总之生成Word文档要比生成PDF文档简单。这里就需要利用python中win32com这个库来进行底层功能的处理,具体情况如下。
安装:
pipinstallpywin32
实现Word转为PDF文档:
fromwin32com.clientimportgencache
fromwin32com.clientimportconstants
importos
curpath=os.path.dirname(__file__)
wordfilename=os.path.join(curpath,'电子简历.docx')
pdffilename=os.path.join(curpath,'电子简历.pdf')
defword_to_pdf(wordPath,pdfPath):#word转pdf
ifos.path.exists(pdfPath):
os.remove(pdfPath)
word=gencache.EnsureDispatch('Word.Application')
doc=word.Documents.Open(wordPath)
doc.ExportAsFixedFormat(pdfPath,constants.wdExportFormatPDF)
word.Quit()
if__name__=='__main__':
word_to_pdf(wordfilename,pdffilename)
现在大家掌握了win32com库的使用了吧,当碰到想要把word转化成pdf的时候,可以操作起来哦~更多Python学习教程请关注IT培训机构:开发教育。
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!