要清理 python-docx 文档中的 sectPr 元素的 pgBorders 属性,可以使用以下代码:

from docx import Document
from docx.oxml import OxmlElement

def clear_pg_borders(document):
    for section in document.sections:
        sectPr = section._sectPr
        if sectPr is not None:
            pgBorders = sectPr.find('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pgBorders')
            if pgBorders is not None:
                bottom = pgBorders.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}bottom')
                if bottom is not None:
                    pgBorders.remove(bottom)
                top = pgBorders.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}top')
                if top is not None:
                    pgBorders.remove(top)
                left = pgBorders.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}left')
                if left is not None:
                    pgBorders.remove(left)
                right = pgBorders.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}right')
                if right is not None:
                    pgBorders.remove(right)

document = Document('example.docx')
clear_pg_borders(document)
document.save('example.docx')

这个函数会遍历文档中的所有节(section),找到每个节的 sectPr 元素(如果存在),然后找到其中的 pgBorders 元素(如果存在),并清理其 bottom、top、left 和 right 属性。最后,保存文档

python-docx 清理文档 sectPr 的 pgBorders 属性

原文地址: https://www.cveoy.top/t/topic/fX3y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录