开发者

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

目录
  • 使用python-docx在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_starphpt(document, bookmark_name):
        """查找标签的起始位置(所在的段落)"""
        doc_element = document.part.element
        bookmarks_list = doc_element.findall('.//' +MyOkryos 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):
                    MyOkryosprint('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 = bookm编程客栈ark.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)
    

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜