-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathet_convert.py
executable file
·129 lines (98 loc) · 3.21 KB
/
et_convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python3
# -- coding: utf-8 -
# **
# * Copyright (c) 2022 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 convert_to(paths, format, abort_on_fails=False):
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)
for path in paths:
abs_path = os.path.realpath(path)
if os.path.isdir(abs_path):
files = [(os.path.join(abs_path, f)) for f in os.listdir(abs_path)]
for file in files:
hr = convert_file(file, docs, format)
_handle_result(hr)
else:
hr = convert_file(abs_path, 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("conversion completed")
except Exception as e:
print(e)
finally:
if pid is not None:
print("kill et")
subprocess.call("kill -9 {}".format(pid), shell=True)
if __name__ == "__main__":
main()