0 Star 0 Fork 2

anglabace/wps-convert

forked from congco/wps-convert 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
wpp.py 3.29 KB
一键复制 编辑 原始数据 按行查看 历史
congco 提交于 2022-10-24 17:19 . feat:convert
#!/usr/bin/python3
# -- coding: utf-8 -
# **
# * Copyright (c) 2020 cong.zheng
# *
# * This file is part of pywpsrpc.
# *
# * This file is distributed under the MIT License.
# * See the LICENSE file for details.
# *
# *
import os
import subprocess
import sys
import argparse
from pywpsrpc.rpcwppapi import (createWppRpcInstance, wppapi)
from pywpsrpc import common
from pywpsrpc.common import (S_OK, QtApp)
formats = {
"pdf": wppapi.PpSaveAsFileType.ppSaveAsPDF,
}
pid = None
class ConvertException(Exception):
def __init__(self, text, hr):
self.text = text
self.hr = hr
def __str__(self):
return """Convert failed:
Details: {}
ErrCode: {}
""".format(self.text, hex(self.hr & 0xFFFFFFFF))
def check_call(funcName, hr, value=None):
if not common.SUCCEEDED(hr):
print("call {} failed with code: {}".format(funcName, hr))
sys.exit(-1)
if value != None:
print("{}: {}".format(funcName, value))
else:
print("{}: <ok>".format(funcName))
def convert_to(paths, format, abort_on_fails=False):
args = ['wpp.py', '-f', format, paths]
QtApp(args)
hr, rpc = createWppRpcInstance()
if hr != S_OK:
raise ConvertException("Can't create the rpc instance", hr)
hr, app = rpc.getWppApplication()
if hr != S_OK:
raise ConvertException("Can't get the application", hr)
global pid
hr, pid = rpc.getProcessPid()
if hr != S_OK:
raise ConvertException("Can't get the PID", hr)
print("PID:{}".format(pid))
# we don't need the gui
# Call 'put_Visible()' failed with 0x80010105
# app.Visible = wppapi.MsoTriState.msoFalse
docs = app.Presentations
def _handle_result(hr):
if abort_on_fails and hr != S_OK:
raise ConvertException("convert_file failed", hr)
hr = convert_file(paths, docs, format)
_handle_result(hr)
app.Quit()
def convert_file(file, docs, format):
hr, doc = docs.Open(file, ReadOnly=True)
if hr != S_OK:
return hr
out_dir = os.path.dirname(os.path.realpath(file)) + "/out"
os.makedirs(out_dir, exist_ok=True)
# you have to handle if the new_file already exists
new_file = out_dir + "/" + os.path.splitext(os.path.basename(file))[0] + "." + format
ret = doc.SaveAs(new_file, formats[format])
# always close the doc
doc.Close()
return ret
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--format", "-f",
required=True,
metavar="<DOC_TYPE>",
choices=["pdf"],
help="convert to <DOC_TYPE>,")
parser.add_argument("--abort", "-a",
action="store_true",
help="abort if one convert fails")
parser.add_argument("path",
metavar="<path>",
nargs='+',
help="the <path> can be one or more file or folder")
args = parser.parse_args()
qApp = QtApp(sys.argv)
try:
convert_to(args.path, args.format, args.abort)
print("convert over")
except Exception as e:
print(e)
finally:
# ubuntu
if pid is not None:
subprocess.Popen("kill -9 {}".format(pid), shell=True).wait()
if __name__ == "__main__":
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/anglabace/wps-convert.git
git@gitee.com:anglabace/wps-convert.git
anglabace
wps-convert
wps-convert
main

搜索帮助