zoukankan      html  css  js  c++  java
  • python实现word转pdf

    环境:python3、工具pycharm、依赖包pywin32

    例子:work转pdf(单个和批量转换当前目录的word文件)

    from win32com.client import constants,gencache
    import os #目录的操作
    def createpdf(wordPath,pdfPath):
        word=gencache.EnsureDispatch('Word.Application')
        doc=word.Documents.Open(wordPath,ReadOnly=1)
        #转换方法
        doc.ExportAsFixedFormat(pdfPath,constants.wdExportFormatPDF)
        word.Quit()
    
    #单个文件转换
    #createpdf('C:/Users/Administrator/PycharmProjects/Project3/info.docx','C:/Users/Administrator/PycharmProjects/Project3/info.pdf')
    
    #多个文件的转换
    print(os.listdir('.')) #当前文件夹下的所有文件
    wordfiles=[]
    for file in os.listdir('.'):
        if file.endswith(('.doc','.docx')): #通过后缀找出所有的workd文件
            wordfiles.append(file)
    print(wordfiles)
    
    for file in wordfiles:
        #获取文件路径
        filepath=os.path.abspath(file)
        index=filepath.rindex('.')
        #通过截取获取pdfpath
        pdfpath=filepath[:index]+'.pdf'
        print(pdfpath)
        createpdf(filepath,pdfpath)
    

    结果显示:

     

      

    做一个决定,并不难,难的是付诸行动,并且坚持到底。
  • 相关阅读:
    JS4
    JS3
    JS2
    JS1
    Dos命令
    面向对象的复习
    9.14Css
    9.13列表的用法
    9.12Css
    9.11Css
  • 原文地址:https://www.cnblogs.com/wukc/p/14899614.html
Copyright © 2011-2022 走看看