Skip to content

Commit caad0fd

Browse files
Merge pull request #1 from darkvisiontech/fix-svg-suffix
Fix svg suffix
2 parents bc2ce94 + 4007497 commit caad0fd

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/docx/image/svg.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,28 @@ def default_ext(self):
3333
"""
3434
return "svg"
3535

36+
@classmethod
37+
def _parse_svg_dims(cls, value):
38+
svg_dims_dict = {
39+
'px': 1,
40+
'cm': 37.7952755906,
41+
'pt': 1 / 0.75,
42+
'in': 96,
43+
'mm': 3.77952755906,
44+
}
45+
try:
46+
return int(value)
47+
except ValueError:
48+
for unit, factor in svg_dims_dict.items():
49+
if value.endswith(unit):
50+
return int(float(value[:-len(unit)]) * factor)
51+
3652
@classmethod
3753
def _dimensions_from_stream(cls, stream):
3854
stream.seek(0)
3955
data = stream.read()
4056
root = ET.fromstring(data)
41-
# FIXME: The width could be expressed as '4cm'
4257
# See https://www.w3.org/TR/SVG11/struct.html#NewDocument
43-
width = int(root.attrib["width"])
44-
height = int(root.attrib["height"])
58+
width = cls._parse_svg_dims(root.attrib["width"])
59+
height = cls._parse_svg_dims(root.attrib["height"])
4560
return width, height

0 commit comments

Comments
 (0)