Skip to content

section.bottom_margin fails #1335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
VTroyanGolovyan opened this issue Feb 7, 2024 · 3 comments · Fixed by BlackBoiler/python-docx#23
Closed

section.bottom_margin fails #1335

VTroyanGolovyan opened this issue Feb 7, 2024 · 3 comments · Fixed by BlackBoiler/python-docx#23

Comments

@VTroyanGolovyan
Copy link

section.bottom_margin for googledocs document
self.inner_width, self.inner_height = inner_sizes(section)
File "/Users/vhdev/PycharmProjects/word_split/help.py", line 8, in inner_sizes
print(section.bottom_margin())
File "/Users/vhdev/PycharmProjects/word_split/venv/lib/python3.9/site-packages/docx/section.py", line 42, in bottom_margin
return self._sectPr.bottom_margin
File "/Users/vhdev/PycharmProjects/word_split/venv/lib/python3.9/site-packages/docx/oxml/section.py", line 185, in bottom_margin
return pgMar.bottom
File "/Users/vhdev/PycharmProjects/word_split/venv/lib/python3.9/site-packages/docx/oxml/xmlchemy.py", line 211, in get_attr_value
return self._simple_type.from_xml(attr_str_value)
File "/Users/vhdev/PycharmProjects/word_split/venv/lib/python3.9/site-packages/docx/oxml/simpletypes.py", line 26, in from_xml
return cls.convert_from_xml(xml_value)
File "/Users/vhdev/PycharmProjects/word_split/venv/lib/python3.9/site-packages/docx/oxml/simpletypes.py", line 319, in convert_from_xml
return Twips(int(str_value))
ValueError: invalid literal for int() with base 10: '1133.8582677165355'

@scanny
Copy link
Contributor

scanny commented Feb 7, 2024

Hmm, interesting. That string "1133.8582677165355" is not a valid value for that XML attribute according to the XML Schema. https://github.com/python-openxml/python-docx/blob/master/ref/xsd/shared-commonSimpleTypes.xsd#L104

But I expect Word accepts it. I'll register it here as something to accommodate. In the meantime you'll need to work around it somehow. I think I'd be inclined to pro-actively patch that value in my code before trying to access the margins.

You can access the w:sectPr element from Section._sectPr and you XPath and lxml calls from there to find and fix any of those before calling the margin attributes.

@totallylosthere
Copy link

totallylosthere commented Feb 16, 2024

"You can access the w:sectPr element from Section._sectPr and you XPath and lxml calls from there to find and fix any of those before calling the margin attributes." - This is a little advanced for a non programmer like me, lol

I did get into your code and put the str_value variable inside a float() function, nested inside a round() function, nested inside the int() function where you were directly passing str_value as its argument. So it looks like
return Twips(int(round(float(str_value))))

I would however like to know how to fix my document margins float values in code without having to hack yours, haha

@scanny
Copy link
Contributor

scanny commented Apr 29, 2024

Fixed in v1.1.1 circa Apr 30, 2024.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants