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

Commit

Permalink
Merge pull request #23 from PolicyStat/issue_23
Browse files Browse the repository at this point in the history
If an image has a height or width of 0 convert_image will fail
  • Loading branch information
jlward committed Apr 1, 2013
2 parents e2467e5 + e8ee18b commit ed70d47
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Expand Up @@ -2,6 +2,10 @@
Changelog 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 * 0.1.10
* Added a user facing version * Added a user facing version
* 0.1.9 * 0.1.9
Expand Down
17 changes: 12 additions & 5 deletions docx2html/core.py
Expand Up @@ -73,6 +73,9 @@ def get_namespace(el, namespace):


def convert_image(target, image_size): def convert_image(target, image_size):
_, extension = os.path.splitext(os.path.basename(target)) _, 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. # All the image types need to be converted to gif.
invalid_extensions = ( invalid_extensions = (
'.bmp', '.bmp',
Expand Down Expand Up @@ -1279,11 +1282,15 @@ def get_p_data(p, meta_data, is_td=False):
else: else:
target = meta_data.relationship_dict[image_id] target = meta_data.relationship_dict[image_id]
width, height = _get_image_size_from_image(target) width, height = _get_image_size_from_image(target)
p_text += '<img src="%s" height="%d" width="%d"/>' % ( # Make sure the width and height are not zero
src, if all((width, height)):
height, p_text += '<img src="%s" height="%d" width="%d" />' % (
width, 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 # This function does not return a p tag since other tag types need this as
# well (td, li). # well (td, li).
Expand Down
Binary file added docx2html/fixtures/bullet_go_gray.png
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
@@ -1,17 +1,19 @@
import os
import mock import mock
from itertools import chain from itertools import chain
from lxml import etree from lxml import etree
from copy import copy from copy import copy


from docx2html.core import ( from docx2html.core import (
_is_top_level_upper_roman, _is_top_level_upper_roman,
convert_image,
create_html, create_html,
get_font_size, get_font_size,
get_image_id, get_image_id,
get_li_nodes, get_li_nodes,
get_namespace,
get_relationship_info, get_relationship_info,
get_style_dict, get_style_dict,
get_namespace,
is_last_li, is_last_li,
) )
from docx2html.tests.document_builder import DocxBuilder as DXB 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, {}) 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): class ListWithContinuationTestCase(_TranslationTestCase):
expected_output = ''' expected_output = '''
<html> <html>
Expand Down

0 comments on commit ed70d47

Please sign in to comment.