使用 python-docx 访问书签

Map*_*Man 5 python ms-word python-docx

我正在使用 python-docx 模块读取和编辑 .docm 文件,该文件包含书签,如何访问已使用该模块存储的所有书签,doc 对象中似乎没有任何方法。

bru*_*off 1

正如 @D Malan 所评论的,现在到了 2021 年 11 月,它仍然是python-docx 中的一个未决问题。

同时我们可以接受我们自己的实现。

docxbookmark.py请在可导入的文件夹中创建一个名为的文件:

from docx.document import Document as _innerdoclass
from docx import Document as _innerdocfn
from docx.oxml.shared import qn
from lxml.etree import Element as El

class Document(_innerdoclass):
    def _bookmark_elements(self, recursive=True):
        if recursive:
            startag = qn('w:start')
            bkms = []
            def _bookmark_elements_recursive(parent):
                if parent.tag == startag:
                    bkms.append(parent)
                for el in parent:
                    _bookmark_elements_recursive(el)
            _bookmark_elements_recursive(self._element)
            return bkms
        else:
            return self._element.xpath('//'+qn('w:bookmarkStart'))
    def bookmark_names(self):
        """
        Gets a list of bookmarks
        """
        return [v for bkmkels in self._bookmark_elements() for k,v in bkmkels.items() if k.endswith('}name')]
    def add_bookmark(self, bookmarkname):
        """
        Adds a bookmark with bookmark with name bookmarkname to the end of the file
        """
        el = [el for el in self._element[0] if el.tag.endswith('}p')][-1]
        el.append(El(qn('w:bookmarkStart'),{qn('w:id'):'0',qn('w:name'):bookmarkname}))
        el.append(El(qn('w:bookmarkEnd'),{qn('w:id'):'0'}))
    def __init__(self, innerDocInstance = None):
        super().__init__(Document, None)
        if innerDocInstance is not None and type(innerDocInstance) is _innerdoclass:
            self.__body = innerDocInstance.__body
            self._element = innerDocInstance._element
            self._part = innerDocInstance._part

def DocumentCreate(docx=None):
    """
    Return a |Document| object loaded from *docx*, where *docx* can be
    either a path to a ``.docx`` file (a string) or a file-like object. If
    *docx* is missing or ``None``, the built-in default document "template"
    is loaded.
    """
    return Document(_innerdocfn(docx))
Run Code Online (Sandbox Code Playgroud)

现在我们可以像旧的一样使用我们的外观实现,以及那些新的add_bookmarkbookmark_names.

要在新文件中添加书签,请导入我们的实现并add_bookmark在文档对象上使用:

from docxbookmark import DocumentCreate as Document

doc = Document()
document.add_paragraph('First Paragraph')
document.add_bookmark('FirstBookmark')
document.add_paragraph('Second Paragraph')
document.save('docwithbookmarks.docx')
Run Code Online (Sandbox Code Playgroud)

要查看文档中的书签,请导入我们的实现并bookmark_names在文档对象上使用:

from docxbookmark import DocumentCreate as Document

doc = Document('docwithbookmarks.docx')
doc.bookmark_names()
Run Code Online (Sandbox Code Playgroud)

返回的列表比其他对象更简单,它仅显示字符串而不是对象。有一个内部_bookmark_elements将返回与 python-docx 对象不同的 lxml 节点。

仅进行了一些测试,在很多情况下可能不起作用。如果它不起作用,请在评论中告诉我。