Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

If an image has a height or width of 0 convert_image will fail #23

Merged
merged 3 commits into from
Apr 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* 0.1.11
* Sometimes in the OOXML an image will have a height or width of 0. If this
happens we are now ignoring the height and width in the OOXML and using
the full image instead.
* 0.1.10
* Added a user facing version
* 0.1.9
Expand Down
17 changes: 12 additions & 5 deletions docx2html/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def get_namespace(el, namespace):

def convert_image(target, image_size):
_, extension = os.path.splitext(os.path.basename(target))
# If the image size has a zero in it early return
if image_size and not all(image_size):
return target
# All the image types need to be converted to gif.
invalid_extensions = (
'.bmp',
Expand Down Expand Up @@ -1279,11 +1282,15 @@ def get_p_data(p, meta_data, is_td=False):
else:
target = meta_data.relationship_dict[image_id]
width, height = _get_image_size_from_image(target)
p_text += '<img src="%s" height="%d" width="%d"/>' % (
src,
height,
width,
)
# Make sure the width and height are not zero
if all((width, height)):
p_text += '<img src="%s" height="%d" width="%d" />' % (
src,
height,
width,
)
else:
p_text += '<img src="%s" />' % src

# This function does not return a p tag since other tag types need this as
# well (td, li).
Expand Down
Binary file added docx2html/fixtures/bullet_go_gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion docx2html/tests/test_xml.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import os
import mock
from itertools import chain
from lxml import etree
from copy import copy

from docx2html.core import (
_is_top_level_upper_roman,
convert_image,
create_html,
get_font_size,
get_image_id,
get_li_nodes,
get_namespace,
get_relationship_info,
get_style_dict,
get_namespace,
is_last_li,
)
from docx2html.tests.document_builder import DocxBuilder as DXB
Expand Down Expand Up @@ -356,6 +358,46 @@ def test_get_relationship_info(self):
self.assertEqual(relationship_info, {})


class ImageNoSizeTestCase(_TranslationTestCase):
relationship_dict = {
'rId0': os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'..',
'fixtures',
'bullet_go_gray.png',
)
}
image_sizes = {
'rId0': (0, 0),
}
expected_output = '''
<html>
<p>
<img src="%s" />
</p>
</html>
''' % relationship_dict['rId0']

@staticmethod
def image_handler(image_id, relationship_dict):
return relationship_dict.get(image_id)

def get_xml(self):
drawing = DXB.drawing('rId0')
tags = [
drawing,
]
body = ''
for el in tags:
body += el

xml = DXB.xml(body)
return etree.fromstring(xml)

def test_convert_image(self):
convert_image(self.relationship_dict['rId0'], self.image_sizes['rId0'])


class ListWithContinuationTestCase(_TranslationTestCase):
expected_output = '''
<html>
Expand Down