• 技术文章 >Python技术 >Python基础教程

    python中的win32com库是什么?

    十一十一2021-01-08 11:16:16原创15886

    我们在日常生活中有时候直接生成PDF比较困难,可以换个思路,先生成Word文档,再使用win32com库将Word文档转为PDF文档,这样的转换基本上100%保留了Word的样式。总之生成Word文档要比生成PDF文档简单。这里就需要利用python中win32com这个库来进行底层功能的处理,具体情况如下。

    安装:

    1

    pip install pywin32

    实现Word转为PDF文档:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    from win32com.client import gencache

    from win32com.client import constants

    import os

    curpath = os.path.dirname(__file__)

    wordfilename = os.path.join(curpath, '电子简历.docx')

    pdffilename = os.path.join(curpath, '电子简历.pdf')

    def word_to_pdf(wordPath, pdfPath): #word转pdf

    if os.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的时候,可以操作起来哦~

    专题推荐:win32com库是什么
    上一篇:Python中Selenium库如何调用浏览器? 下一篇:如何使用Pandas处理Excel?

    相关文章推荐

    • python中glob库是什么?• Python中Selenium库如何调用浏览器?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网