python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python-docx在word文档中查找书签,并在书签处写入数据

使用python-docx在word文档中查找书签,并在书签处写入数据方式

作者:hooyying

使用python-docx库,可在Word文档中查找书签并写入数据。首先打开文档,通过bookmarks属性获取书签对象,然后使用书签名定位书签位置,最后将数据写入书签处。这种方法适合自动化填充Word模板等场景

使用python-docx在word文档中查找书签,并在书签处写入数据

来看实例

from docx import Document,oxml
from docx.oxml.shared import qn
from docx.text.paragraph import Paragraph
from docx.oxml 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):
                print('par不是CT_P类型,返回2')
                return 2
            else:
                print(type(par))
                #return par, bookmark.get(qn('w:id'))
                return par
    print('没找到书签,返回1')
    return 1
#查找书签的结束位置(所在的段落)
def get_bookmark_par_element_end(document, bookmark_id):
    doc_element = document.part.element
    bookmarks_list = doc_element.findall('.//' + qn('w:bookmarkEnd'))
    for bookmark in bookmarks_list:
        name = bookmark.get(qn('w:id'))
        if name == bookmark_id:
            par = bookmark.getparent()
            if not isinstance(par,oxml.CT_P):
                return 2
            else:
                return par
    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)
    print(new_para)
    print(text)
    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 = get_bookmark_par_element_start(document, bookmark_name)
    if bookmark_par == 1 or bookmark_par==2:
        print('未到书签或类型错误')
        return
    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]
        result_with_space = " ".join(_text)
        insert_paragraph_after(base_paragraphs, result_with_space)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文