aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-14 17:42:17 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-14 17:42:17 +0000
commitb2db0099904e8a7e9d580e2a591b3914e187821a (patch)
tree1e5b66f207c2e99e8fbf06ad77f9673d66b73eff
parent9ad98b4bbed0c67f25576187996e7e1d534f6a90 (diff)
Add mammoth script
-rw-r--r--scripts/mammoth42
-rw-r--r--setup.py1
2 files changed, 43 insertions, 0 deletions
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",