如何使用Python Docx更改表中的线厚度

发布于 2025-02-03 09:31:21 字数 310 浏览 2 评论 0原文

line厚度1

表格中的厚度2

我当前正在尝试更改Microsoft Word文档中表的线厚度。该桌子带有1 pt的默认线厚度,我正在尝试使用Python Docx找到一种将行厚度更改为2pt的方法。

Line thickness 1 within a table

Line thickness 2 within a table

I am currently trying to change the line thicknesss of a table within a Microsoft word document. The table comes with a default line thickness of 1 pt, I am trying to find a way to change the line thickness to 2pt using python docx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2025-02-10 09:31:21

设置表/单元边框的文档可在此处提供: http://officeopenxml.com/wptpableborders.php

我已经重用可用的现有代码设置在此处回答的表单元边框 a>并将其修改为设置单​​个表属性。这将设置表中所有边界的统一属性,并将其存储在表级别(而不是在单个单元格级别):

from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.table import Table
from docx import Document

def set_table_border(table: Table, **kwargs):
    """
    Sets table border
    Usage:

    set_table_border(
        table,
        top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
        bottom={"sz": 12, "color": "#00FF00", "val": "single"},
        start={"sz": 24, "val": "dashed", "shadow": "true"},
        end={"sz": 12, "val": "dashed"},
    )
    """
    tbl  = table._tbl
    tblPr = tbl.tblPr

    # check for tag existnace, if none found, then create one
    tblBorders = tblPr.first_child_found_in("w:tblBorders")
    if tblBorders is None:
        tblBorders = OxmlElement('w:tblBorders')
        tblPr.append(tblBorders)

    # list over all available tags
    for edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):
        edge_data = kwargs.get(edge)
        if edge_data:
            tag = 'w:{}'.format(edge)

            # check for tag existnace, if none found, then create one
            element = tblBorders.find(qn(tag))
            if element is None:
                element = OxmlElement(tag)
                tblBorders.append(element)

            # looks like order of attributes is important
            for key in ["sz", "val", "color", "space", "shadow"]:
                if key in edge_data:
                    element.set(qn('w:{}'.format(key)), str(edge_data[key]))

if __name__ == "__main__":
    doc = Document()
    t = doc.add_table(rows=1, cols=3)

    border_prop = {
        'sz': '16', # table border thickness (8=1pt => 16 = 2pt)
        'val': 'single', # line style
        'color': 'auto' # border color
    }
    set_table_border(t, top=border_prop, bottom=border_prop,
                        end=border_prop, start=border_prop,
                        insideH=border_prop, insideV=border_prop)

    doc.save('borders.docx')

documentation for setting table/cell border is available here: http://officeopenxml.com/WPtableBorders.php

i've reused the existing code available to set table cell border answered here https://stackoverflow.com/a/49615968/5736491 and modified it to set single table property. this will set the uniform property for all borders within the table, and it is stored at the table level (instead of at the individual cell level):

from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.table import Table
from docx import Document

def set_table_border(table: Table, **kwargs):
    """
    Sets table border
    Usage:

    set_table_border(
        table,
        top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
        bottom={"sz": 12, "color": "#00FF00", "val": "single"},
        start={"sz": 24, "val": "dashed", "shadow": "true"},
        end={"sz": 12, "val": "dashed"},
    )
    """
    tbl  = table._tbl
    tblPr = tbl.tblPr

    # check for tag existnace, if none found, then create one
    tblBorders = tblPr.first_child_found_in("w:tblBorders")
    if tblBorders is None:
        tblBorders = OxmlElement('w:tblBorders')
        tblPr.append(tblBorders)

    # list over all available tags
    for edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):
        edge_data = kwargs.get(edge)
        if edge_data:
            tag = 'w:{}'.format(edge)

            # check for tag existnace, if none found, then create one
            element = tblBorders.find(qn(tag))
            if element is None:
                element = OxmlElement(tag)
                tblBorders.append(element)

            # looks like order of attributes is important
            for key in ["sz", "val", "color", "space", "shadow"]:
                if key in edge_data:
                    element.set(qn('w:{}'.format(key)), str(edge_data[key]))

if __name__ == "__main__":
    doc = Document()
    t = doc.add_table(rows=1, cols=3)

    border_prop = {
        'sz': '16', # table border thickness (8=1pt => 16 = 2pt)
        'val': 'single', # line style
        'color': 'auto' # border color
    }
    set_table_border(t, top=border_prop, bottom=border_prop,
                        end=border_prop, start=border_prop,
                        insideH=border_prop, insideV=border_prop)

    doc.save('borders.docx')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文