代码拉取完成,页面将自动刷新
#!/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.rpcetapi import (createEtRpcInstance, etapi)
from pywpsrpc.common import (S_OK, QtApp)
formats = {
"pdf": etapi.XlFixedFormatType.xlTypePDF,
}
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 init_et(abort_on_fails=False):
args = ['et.py', '-f']
QtApp(args)
hr, rpc = createEtRpcInstance()
if hr != S_OK:
raise ConvertException("Can't create the rpc instance", hr)
hr, app = rpc.getEtApplication()
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
app.Visible = False
# docs = app.Workbooks
# return docs
return app
def convert_to(paths, format, abort_on_fails=False):
args = ['et.py', '-f', format, paths]
QtApp(args)
hr, rpc = createEtRpcInstance()
if hr != S_OK:
raise ConvertException("Can't create the rpc instance", hr)
hr, app = rpc.getEtApplication()
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
app.Visible = False
docs = app.Workbooks
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, Password='xxx', 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.ExportAsFixedFormat(formats[format], new_file)
# 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:
if pid is not None:
subprocess.Popen("kill -9 {}".format(pid), shell=True).wait()
if __name__ == "__main__":
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。