0

我正在使用 python-docx 并尝试插入 a<w:bookmarkStart>标签。我没有看到任何直接创建标签的 API 方法。document._document_part因此,我在 Google 上搜索了几个参考资料,以使用该属性访问原始 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>标签的方法?

4

1 回答 1

0

我找到了解决方案。这是一个例子:

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 value
于 2015-08-20T02:38:37.533 回答