aboutsummaryrefslogtreecommitdiff
path: root/mammoth
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-17 20:17:17 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-17 20:17:17 +0000
commitfaae8466ac51b22a9b836103ce6e5c039dfc7eea (patch)
treed746a9cf81abefc626c651756367c97dbbeb77bf /mammoth
parenta2d5ef8d5590bfb7d7b038d7753b8d8b2b013ecd (diff)
Add --output-dir option
Diffstat (limited to 'mammoth')
-rw-r--r--mammoth/__init__.py4
-rw-r--r--mammoth/conversion.py5
-rw-r--r--mammoth/docx/document_xml.py7
3 files changed, 9 insertions, 7 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index 7491fb6..65b8821 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -2,12 +2,12 @@ from .results import Result
from . import docx, conversion, style_reader, lists
-def convert_to_html(fileobj, styles=None):
+def convert_to_html(fileobj, styles=None, convert_image=None):
if styles is None:
styles = _default_styles
return docx.read(fileobj).bind(lambda document:
- conversion.convert_document_element_to_html(document, styles=_read_styles(styles))
+ conversion.convert_document_element_to_html(document, styles=_read_styles(styles), convert_image=convert_image)
)
diff --git a/mammoth/conversion.py b/mammoth/conversion.py
index b324c16..835cb6b 100644
--- a/mammoth/conversion.py
+++ b/mammoth/conversion.py
@@ -73,11 +73,8 @@ class DocumentConverter(object):
def _convert_image(self, image, html_generator):
- image_bytes = image.open()
- try:
+ with image.open() as image_bytes:
encoded_src = base64.b64encode(image_bytes.read()).decode("ascii")
- finally:
- image_bytes.close()
attributes = {
"src": "data:{0};base64,{1}".format(image.content_type, encoded_src)
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index fad4f87..14d36a6 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -1,4 +1,5 @@
import os
+import contextlib
from .. import documents
from .. import results
@@ -138,7 +139,11 @@ def _create_reader(numbering, content_types, relationships, docx_file):
content_type = content_types.find_content_type(image_path)
def open_image():
- return docx_file.open(image_path)
+ image_file = docx_file.open(image_path)
+ if hasattr(image_file, "__exit__"):
+ return image_file
+ else:
+ return contextlib.closing(image_file)
image = documents.image(alt_text=alt_text, content_type=content_type, open=open_image)
return results.success(image)