I am using python-docx and am trying to insert the a <w:bookmarkStart>
tag. I do not see any immediate API method to create the tag. So I googled several references to gain access to the raw XML using the document._document_part
attribute. However, when I attempt to use it, python tells me it does not exist:
>>> import docx
>>> document = docx.Document()
>>> print document._document_part
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Document' object has no attribute '_document_part'
I am using python-docx 0.8.5.
Is there a method to add a <w:bookmarkStart>
tag?
I found the solution. Here's an example:
from docx.oxml.shared import OxmlElement # Necessary Import
tags = document.element.xpath('//w:r') # Locate the right <w:r> tag
tag = tags[0] # Specify which <w:r> tag you want
child = OxmlElement('w:ARBITRARY') # Create arbitrary tag
tag.append(child) # Append in the new tag
To add an attribute:
from docx.oxml.shared import qn
child.set( qn('w:val'), 'VALUE') # Add in the value
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.