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

    验证过ppt也可以合并成pdf文件,确实很高效,什么pdf转换器的再也不用了~

    需要下载模块pywin32,程序中导入模块为win32com。

    # -*- coding:utf-8 -*-
    import os
    from win32com.client import Dispatch,DispatchEx
    from win32com.client import constants
    from win32com.client import gencache
    import re
    
    def getfilenames(filepath='',filelist_out=[],file_ext='all'):
        for fpath, dirs, fs in os.walk(filepath):
            for f in fs:
                fi_d = os.path.join(fpath, f)
                if file_ext == '.doc':
                    if os.path.splitext(fi_d)[1] in ['.doc','.docx']:
                        filelist_out.append(re.sub(r'\','/',fi_d))
                else:
                    if  file_ext == 'all':
                        filelist_out.append(fi_d)
                    elif os.path.splitext(fi_d)[1] == file_ext:
                        filelist_out.append(fi_d)
                    else:
                        pass
            filelist_out.sort()
        return filelist_out
    
    # Word to PDF
    def wordtopdf(filelist,targetpath):
        valueList = []
        try:
            gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
            # 开始转换
            w = Dispatch("Word.Application")
            for fullfilename in filelist:
                (filepath,filename) = os.path.split(fullfilename)  # 分割文件路径和文件名
                softfilename = os.path.splitext(filename)  # 分割文件名和扩展名
                os.chdir(filepath)
                doc = os.path.abspath(filename)
                os.chdir(targetpath)
                pdfname = softfilename[0] + ".pdf"
                output = os.path.abspath(pdfname)
                pdf_name = output
    
                try:
                    doc = w.Documents.Open(doc, ReadOnly=1)
                    doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,
                                    Item=constants.wdExportDocumentWithMarkup,
                                    CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
                except Exception as e:
                    print(e)
                if os.path.isfile(pdf_name):
                    valueList.append(pdf_name)
                else:
                    print('转换失败!')
                    return False
            w.Quit(constants.wdDoNotSaveChanges)
            return valueList
        except TypeError as e:
            print('出错了!')
            print(e)
            return False
    if __name__ == '__main__':
        sourcepath = r"E:/xxx/xxx"  # 指定源路径(Word文档所在路径)
        targetpath = r"E:/xxx/xxx/pdf/"  # 指定目标路径(PDF保存路径)
        filelist = getfilenames(sourcepath,[],'.doc')  # 获取Word文档路径
        valueList = wordtopdf(filelist,targetpath)  # 实现将Word文档批量转换为PDF
        if valueList:
            print("转换成功")
        else:
            print("没有要转换的Word文档或者转换失败!请检查!")
    

      

  • 相关阅读:
    python json 和 pickle的补充 hashlib configparser logging
    go 流程语句 if goto for swich
    go array slice map make new操作
    go 基础
    块级元素 行内元素 空元素
    咽炎就医用药(慢性肥厚性咽炎)
    春季感冒是风寒还是风热(转的文章)
    秋季感冒 咳嗽 怎么选药
    解决IE浏览器“无法显示此网页”的问题
    常用的 css 样式 记录
  • 原文地址:https://www.cnblogs.com/hqczsh/p/12811683.html
Copyright © 2011-2022 走看看