我正在使用python-docx,并尝试插入一个<w:bookmarkStart>标记。我没有看到任何立即创建标记的API方法。因此,我用谷歌搜索了几个参考资料,以便使用document._document_part属性访问原始的XML。但是,当我尝试使用它时,python告诉我它不存在:
>>> 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'我使用的是python-docx 0.8.5。
有没有添加<w:bookmarkStart>标签的方法?
发布于 2015-08-20 10:38:38
我找到了解决方案。下面是一个例子:
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要添加属性,请执行以下操作:
from docx.oxml.shared import qn
child.set( qn('w:val'), 'VALUE') # Add in the valuehttps://stackoverflow.com/questions/31974733
复制相似问题