diff options
| -rw-r--r-- | mammoth/__init__.py | 10 | ||||
| -rw-r--r-- | mammoth/zips.py | 2 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 23 |
3 files changed, 34 insertions, 1 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 297cf24..7735fcf 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -1,5 +1,6 @@ from . import docx, conversion, options, images, underline from .raw_text import extract_raw_text_from_element +from .docx.style_map import write_style_map, read_style_map __all__ = ["convert_to_html", "extract_raw_text", "images", "underline"] @@ -15,6 +16,8 @@ def convert_to_markdown(*args, **kwargs): def convert(fileobj, transform_document=None, id_prefix=None, **kwargs): if transform_document is None: transform_document = lambda x: x + if kwargs.get("style_map") is None: + kwargs["style_map"] = read_style_map(fileobj) return options.read_options(kwargs).bind(lambda convert_options: docx.read(fileobj).map(transform_document).bind(lambda document: conversion.convert_document_element_to_html( @@ -28,3 +31,10 @@ def convert(fileobj, transform_document=None, id_prefix=None, **kwargs): def extract_raw_text(fileobj): return docx.read(fileobj).map(extract_raw_text_from_element) + + +def embed_style_map(fileobj, style_map): + write_style_map(fileobj, style_map) + +def read_embedded_style_map(fileobj): + return read_style_map(fileobj) diff --git a/mammoth/zips.py b/mammoth/zips.py index 432be5d..4d77acb 100644 --- a/mammoth/zips.py +++ b/mammoth/zips.py @@ -42,7 +42,7 @@ def update_zip(fileobj, files): names = set(source.namelist()) | set(files.keys()) for name in names: if name in files: - contents = files[name] + contents = files[name].encode("utf8") else: contents = source.read(name) destination.writestr(name, contents) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index 906f49b..56ef159 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -46,6 +46,23 @@ def empty_paragraphs_are_preserved_if_ignore_empty_paragraphs_is_false(): assert_equal("<p></p>", result.value) assert_equal([], result.messages) + +@istest +def embedded_style_map_is_used_if_present(): + with _copy_of_test_data("single-paragraph.docx") as fileobj: + mammoth.embed_style_map(fileobj, "p => h1") + result = mammoth.convert_to_html(fileobj=fileobj, ignore_empty_paragraphs=False) + assert_equal("<h1>Walking on imported air</h1>", result.value) + assert_equal([], result.messages) + + +@istest +def embedded_style_map_can_be_retrieved(): + with _copy_of_test_data("single-paragraph.docx") as fileobj: + mammoth.embed_style_map(fileobj, "p => h1") + assert_equal("p => h1", mammoth.read_embedded_style_map(fileobj)) + + @istest def warning_if_style_mapping_is_not_understood(): style_map = """ @@ -236,3 +253,9 @@ def can_extract_raw_text(): assert_equal([], result.messages) assert_equal("Apple\n\nBanana\n\n", result.value) + +def _copy_of_test_data(path): + destination = io.BytesIO() + with open(test_path(path), "rb") as source: + shutil.copyfileobj(source, destination) + return destination |
