zoukankan      html  css  js  c++  java
  • 使用python调用wps v9转换office文件到pdf

    #!/usr/bin/python2.6
    # -*- coding: utf-8 -*-
    # pip install timeout-decorator
    
    import os
    import win32com.client
    
    
    # wps使用的是wps2016版本
    # 转换 Word文件档到pdf
    def ConvertDocToPdf(src, dst):
        if not os.path.exists(src):
            print(src + "不存在,无法继续!")
            return False
        os.system('taskkill /im wps.exe')
        # 如果文件存在就删除
        if os.path.exists(dst):
            os.remove(dst)
        o = win32com.client.Dispatch("Kwps.Application")
        o.Visible = False
        doc = o.Documents.Open(src);
        doc.ExportAsFixedFormat(dst, 17)
        o.Quit();
        if os.path.exists(dst):
            return True
        else:
            return False
    
    
    # 转换 Ppt文件档到pdf
    def ConvertPptToPdf(src, dst):
        if not os.path.exists(src):
            print(src + "不存在,无法继续!")
            return False
        os.system('taskkill /im wps.exe')
        # 如果文件存在就删除
        if os.path.exists(dst):
            os.remove(dst)
        wpp = win32com.client.Dispatch("Kwpp.Application")
        # o.Visible=False
        ppt = wpp.Presentations.Open(src)
        ppt.SaveAs(dst, 32)
        ppt.Close()
        wpp.Quit()
        if os.path.exists(dst):
            return True
        else:
            return False
    
    
    # 转换 xls文件档到pdf
    def ConvertXlsToPdf(src, dst):
        if not os.path.exists(src):
            print(src + "不存在,无法继续!")
            return False
        os.system('taskkill /im wps.exe')
        # 如果文件存在就删除
        if os.path.exists(dst):
            os.remove(dst)
        xlApp = win32com.client.Dispatch("Ket.Application")
        excel = xlApp.Workbooks.Open(src)
        excel.ExportAsFixedFormat(0, dst)
        excel.Close()
        xlApp.Quit()
        if os.path.exists(dst):
            return True
        else:
            return False
    
    
    # 当前目录
    d = os.path.dirname(__file__)
    abspath = os.path.abspath(d)
    #
    #
    # 测试用例
    src = abspath + r"/Doc/test.doc"
    dst = abspath + r"/Doc/test.doc.pdf"
    r = ConvertDocToPdf(src, dst)
    print(r)
    
    # 测试用例
    src = abspath + r"/Doc/test.xlsx"
    dst = abspath + r"/Doc/test.xlsx.pdf"
    r = ConvertXlsToPdf(src, dst)
    print(r)
    
    # 测试用例
    src = abspath + r"/Doc/test.pptx"
    dst = abspath + r"/Doc/test.pptx.pdf"
    r = ConvertPptToPdf(src, dst)
    print(r)
  • 相关阅读:
    通过internet网络唤醒主机的方法
    信用风险计量模型
    Vintage、滚动率、迁移率的应用
    (信贷风控十五)评分卡分数切分、授信额度与利率定价
    (信贷风控十)催收评分卡的介绍
    (信贷风控十六)组合评分卡模型
    (信贷风控十四)深度神经网络模型用于评分卡模型(理论)
    (十三)GBDT模型用于评分卡模型python实现
    (信贷风控九)行为评分卡模型python实现
    改变jupyter notebook的主题背景
  • 原文地址:https://www.cnblogs.com/littlehb/p/9684611.html
Copyright © 2011-2022 走看看