Skip to content

Commit 569826e

Browse files
aarbouinalexysdussier
authored andcommitted
Write image height and width in HTML
1 parent 51ffc24 commit 569826e

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

mammoth/images.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ def convert_image(image):
88
attributes = func(image).copy()
99
if image.alt_text:
1010
attributes["alt"] = image.alt_text
11+
if image.size:
12+
attributes["width"] = image.size.width
13+
attributes["height"] = image.size.height
1114

1215
return [html.element("img", attributes)]
1316

tests/cli_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def html_is_written_to_file_if_output_file_is_set():
3636
def inline_images_are_included_in_output_if_writing_to_single_file():
3737
docx_path = test_path("tiny-picture.docx")
3838
result = _local.run(["mammoth", docx_path])
39-
assert_equal(b"""<p><img src="data:image/png;base64,""" + _image_base_64 + b"""" /></p>""", result.output)
39+
assert_equal(b"""<p><img height="10" src="data:image/png;base64,""" + _image_base_64 + b"""" width="10" /></p>""", result.output)
4040

4141

4242
@istest
@@ -50,7 +50,7 @@ def images_are_written_to_separate_files_if_output_dir_is_set():
5050
assert_equal(b"", result.stderr_output)
5151
assert_equal(b"", result.output)
5252
with open(output_path) as output_file:
53-
assert_equal("""<p><img src="1.png" /></p>""", output_file.read())
53+
assert_equal("""<p><img height="10" src="1.png" width="10" /></p>""", output_file.read())
5454

5555
with open(image_path, "rb") as image_file:
5656
assert_equal(_image_base_64, base64.b64encode(image_file.read()))

tests/conversion_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,19 @@ def images_have_alt_tags_if_available():
510510
image_html = parse_xml(io.StringIO(result.value))
511511
assert_equal('It\'s a hat', image_html.attributes["alt"])
512512

513+
@istest
514+
def images_have_width_and_height_tags_if_available():
515+
image = documents.image(
516+
alt_text=None,
517+
content_type="image/png",
518+
size=documents.Size(width=42, height=51),
519+
open=lambda: io.BytesIO(b"abc")
520+
)
521+
result = convert_document_element_to_html(image)
522+
image_html = parse_xml(io.StringIO(result.value))
523+
assert_equal('42', image_html.attributes["width"])
524+
assert_equal('51', image_html.attributes["height"])
525+
513526

514527
@istest
515528
def can_define_custom_conversion_for_images():

tests/images_tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ def data_uri_encodes_images_in_base64():
1717
image = mammoth.documents.Image(
1818
alt_text=None,
1919
content_type="image/jpeg",
20+
size=mammoth.documents.Size(width=800, height=600),
2021
open=lambda: io.BytesIO(image_bytes),
2122
)
2223

2324
result = mammoth.images.data_uri(image)
2425

2526
assert_that(result, contains(
26-
has_properties(attributes={"src": "data:image/jpeg;base64,YWJj"}),
27+
has_properties(attributes={
28+
"src": "data:image/jpeg;base64,YWJj",
29+
"width": "800",
30+
"height": "600",
31+
}),
2732
))

tests/mammoth_tests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,23 @@ def warning_if_style_mapping_is_not_understood():
112112
def inline_images_referenced_by_path_relative_to_part_are_included_in_output():
113113
with open(test_path("tiny-picture.docx"), "rb") as fileobj:
114114
result = mammoth.convert_to_html(fileobj=fileobj)
115-
assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
115+
assert_equal("""<p><img height="10" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" width="10" /></p>""", result.value)
116116
assert_equal([], result.messages)
117117

118118

119119
@istest
120120
def inline_images_referenced_by_path_relative_to_base_are_included_in_output():
121121
with open(test_path("tiny-picture-target-base-relative.docx"), "rb") as fileobj:
122122
result = mammoth.convert_to_html(fileobj=fileobj)
123-
assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
123+
assert_equal("""<p><img height="10" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" width="10" /></p>""", result.value)
124124
assert_equal([], result.messages)
125125

126126

127127
@istest
128128
def images_stored_outside_of_document_are_included_in_output():
129129
with open(test_path("external-picture.docx"), "rb") as fileobj:
130130
result = mammoth.convert_to_html(fileobj=fileobj)
131-
assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
131+
assert_equal("""<p><img height="10" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" width="10" /></p>""", result.value)
132132
assert_equal([], result.messages)
133133

134134

@@ -173,7 +173,7 @@ def convert_image(image):
173173

174174
with open(test_path("tiny-picture.docx"), "rb") as fileobj:
175175
result = mammoth.convert_to_html(fileobj=fileobj, convert_image=convert_image)
176-
assert_equal("""<p><img src="iV,image/png" /></p>""", result.value)
176+
assert_equal("""<p><img height="10" src="iV,image/png" width="10" /></p>""", result.value)
177177
assert_equal([], result.messages)
178178

179179

0 commit comments

Comments
 (0)