python-docx在书签后追加段落内容

python-docx的官方文档:https://python-docx.readthedocs.io/en/latest/

示例代码

比如文档 “测试1.docx” 的内容如下:
python-docx在书签后追加段落内容_第1张图片
想要在【书签a】的下面追加一些内容时,可以使用如下方法:

from docx import Document, oxml
from docx.oxml.shared import qn
from docx.text.paragraph import Paragraph
from docx.oxml.xmlchemy import OxmlElement


def get_bookmark_par_element_start(document, bookmark_name):
    """查找标签的起始位置(所在的段落)"""
    doc_element = document.part.element
    bookmarks_list = doc_element.findall('.//' + qn('w:bookmarkStart'))
    for bookmark in bookmarks_list:
        name = bookmark.get(qn('w:name'))
        if name == bookmark_name:
            par = bookmark.getparent()
            if not isinstance(par, oxml.CT_P):
                return 2
            else:
                return par, bookmark.get(qn('w:id'))
    return 1


def insert_paragraph_after(paragraph, text=""):
    """在paragraph后添加新的段落"""
    new_p = OxmlElement("w:p")
    paragraph._p.addnext(new_p)
    new_para = Paragraph(new_p, paragraph._parent)
    new_para.add_run(text)
    return new_para


def set_bookmark_values(document, bookmark_name, text_list: list):
    """
    :param document:
    :param bookmark_name:
    :param text_list: 待添加的文本列表
    :return:
    """
    bookmark_par, _id = get_bookmark_par_element_start(document, bookmark_name)
    bookmark_par_parent = bookmark_par.getparent()
    index = bookmark_par_parent.index(bookmark_par)
    for _i, _text in enumerate(text_list):
        base_paragraphs = document.paragraphs[index + _i]
        insert_paragraph_after(base_paragraphs, _text)


def main():
    path = '测试1.docx'
    doc = Document(path)
    set_bookmark_values(doc, '书签a', ['准备添加到书签后的段落1', '准备添加到书签后的段落2', "准备添加到书签后的段落3"])
    doc.save('测试2.docx')  # 保存后导出


if __name__ == '__main__':
    main()

追加段落后的 “测试2.docx” 的内容如下:

python-docx在书签后追加段落内容_第2张图片

你可能感兴趣的:(python,自动化办公,python,数学建模,开发语言)