Skip to content

Non-integer font sizes #1475

Open
Open
@ThomasProctor

Description

@ThomasProctor

version: 1.1.2

I have multiple documents (which, unfortunately, I'm not able to share directly) that I can open just fine in word, but when I try to access a run.font.size on a certain cursed run, I get an error like this: ValueError: invalid literal for int() with base 10: '36.56250317891439'

Here's a full traceback:

---> 40     return run.font.size
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-46f9411e-ed8b-4f5f-ac48-4707bd9781bd/lib/python3.10/site-packages/docx/text/font.py:273, in Font.size(self)
    272     return None
--> 273 return rPr.sz_val
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-46f9411e-ed8b-4f5f-ac48-4707bd9781bd/lib/python3.10/site-packages/docx/oxml/text/font.py:300, in CT_RPr.sz_val(self)
    299     return None
--> 300 return sz.val
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-46f9411e-ed8b-4f5f-ac48-4707bd9781bd/lib/python3.10/site-packages/docx/oxml/xmlchemy.py:258, in RequiredAttribute._getter.<locals>.get_attr_value(obj)
    255     raise InvalidXmlError(
    256         "required '%s' attribute not present on element %s" % (self._attr_name, obj.tag)
    257     )
--> 258 return self._simple_type.from_xml(attr_str_value)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-46f9411e-ed8b-4f5f-ac48-4707bd9781bd/lib/python3.10/site-packages/docx/oxml/simpletypes.py:26, in BaseSimpleType.from_xml(cls, xml_value)
     24 @classmethod
     25 def from_xml(cls, xml_value: str) -> Any:
---> 26     return cls.convert_from_xml(xml_value)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-46f9411e-ed8b-4f5f-ac48-4707bd9781bd/lib/python3.10/site-packages/docx/oxml/simpletypes.py:265, in ST_HpsMeasure.convert_from_xml(cls, str_value)
    264     return ST_UniversalMeasure.convert_from_xml(str_value)
--> 265 return Pt(int(str_value) / 2.0)
ValueError: invalid literal for int() with base 10: '36.56250317891439'

I'm guessing this is something similar to an earlier issue, #1335, where the XML schema says this is invalid but there must be some way it ends up happening, but it doesn't break Microsoft Word. Given that this int is being instantly converted to a float, it seems likely that it will be completely harmless to just change int to float on docx/oxml/simpletypes.py:265

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions