blob: 16563e9b9ab32b2ff8afe6a77583cdeda0524425 (
plain)
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
|
#!/usr/bin/env python
import argparse
import contextlib
import sys
import mammoth
def main():
args = _parse_args()
if args.styles is None:
styles = None
else:
with open(args.styles) as styles_fileobj:
styles = styles_fileobj.read()
with _open_output(args.output) as output:
with open(args.path, "rb") as docx_fileobj:
result = mammoth.convert_to_html(docx_fileobj, styles=styles)
for message in result.messages:
sys.stderr.write(message.message)
sys.stderr.write("\n")
output.write(result.value)
@contextlib.contextmanager
def _open_output(name):
if name is None:
try:
yield Output(sys.stdout)
finally:
sys.stdout.flush()
else:
with open(name, "w") as output:
yield Output(output)
class Output(object):
def __init__(self, fileobj):
self._fileobj = fileobj
def write(self, value):
if sys.version_info[0] <= 2:
self._fileobj.write(value.encode("utf8"))
else:
self._fileobj.write(value)
def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("path")
parser.add_argument("output", nargs="?")
parser.add_argument("--styles", required=False)
return parser.parse_args()
if __name__ == "__main__":
main()
|