From b2db0099904e8a7e9d580e2a591b3914e187821a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 14 Dec 2013 17:42:17 +0000 Subject: Add mammoth script --- scripts/mammoth | 42 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 43 insertions(+) create mode 100644 scripts/mammoth diff --git a/scripts/mammoth b/scripts/mammoth new file mode 100644 index 0000000..4de4120 --- /dev/null +++ b/scripts/mammoth @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import argparse +import contextlib +import sys + +import mammoth + + +def main(): + args = _parse_args() + + with _open_output(args.output) as output: + with open(args.path, "rb") as docx_fileobj: + result = mammoth.convert_to_html(docx_fileobj) + 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 sys.stdout + finally: + sys.stdout.flush() + else: + with open(name, "w") as output: + yield output + + +def _parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("path") + parser.add_argument("output", nargs="?") + return parser.parse_args() + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 80874b5..00d621e 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ setup( author_email='mike@zwobble.org', url='http://github.com/mwilliamson/python-mammoth', packages=['mammoth', 'mammoth.docx', 'mammoth.style_reader'], + scripts=["scripts/mammoth"], keywords="docx word office clean html", install_requires=[ "parsimonious>=0.5,<0.6", -- cgit v1.2.3