I'm trying to write my python scripts using python-docx to add equations to docx files. Since this is not supported by python-docx yet, I need to modify the OOXML directly using lxml. I'd need to add some tags so the resulting XML looks like this:
<m:r>
<m:t>(5−x)</m:t>
</m:r>
I can't create a new naked <m:r>
node using lxml. Using something like the following code
new_node = etree.Element('{m}r', nsmap={'m': ''})
gives me a <m:r xmlns:m="m">
node, except I don't need the xmlns part. (python-docx handles the remaining XML operations for me, and if the xmlns is there, the docx file actually wouldn't be recognised by MS Word.) Is there any way to make a naked <m:r>
node?
python-docx
is aware of the "m": "http://schemas.openxmlformats.org/officeDocument/2006/math"
namespace-prefix mapping: https://github.com/python-openxml/python-docx/blob/master/docx/oxml/ns.py#L18
So you can use python-docx
internals to create such an element:
from docx.oxml import OxmlElement
r = OxmlElement("m:r")
You can then use that element like you would any other lxml
element, like appending or inserting it as a child of some other element.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.