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)

输出结果如下:

python中的win32com库是什么?

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。