如何使用python-docx库设置表格单元格的边距(How to set cell margins of tables using python docx)

本文介绍如何使用Python-docx库自定义调整Word文档中表格的边距,包括顶、底、左、右边距的设置方法,适用于需要精确控制文档布局的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.插入表格后,目前python-docx没有官方方法将单元格边距设置成自己想要的距离。

2.下面代码可实现调整单元格边距

from docx.table import _Cell
def set_cell_margins(cell: _Cell, **kwargs):
    """
    cell:  actual cell instance you want to modify

    usage:

        set_cell_margins(cell, top=50, start=50, bottom=50, end=50)

    provided values are in twentieths of a point (1/1440 of an inch).
    read more here: http://officeopenxml.com/WPtableCellMargins.php
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    tcMar = OxmlElement('w:tcMar')

    for m in [
        "top",
        "start",
        "bottom",
        "end",
    ]:
        if m in kwargs:
            node = OxmlElement("w:{}".format(m))
            node.set(qn('w:w'), str(kwargs.get(m)))
            node.set(qn('w:type'), 'dxa')
            tcMar.append(node)

    tcPr.append(tcMar)

2.更多openxml tags可点击 http://officeopenxml.com/WPtableCellMargins.php

Elements:

ElementDescription
topSpecifies the amount of space left between the top of the cell contents and the top border of all cells. If omitted, the table will have no top padding unless a cell overrides the default.

Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.76.

bottomSpecifies the amount of space left between the bottom of the cell contents and the border of all cells. If omitted, the table will have no bottom padding unless a cell overrides the default.

Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.5.

startSpecifies the amount of space displayed to the left for left-to-right tables and right for right-to-left tables. If omitted, the table will have 115 twentieths of a point (0.08 inches) of left padding unless a cell overrides the default.

Note: In the previous version of the standard, this element was left.

Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.35.

endSpecifies the amount of space displayed on the right for left-to-right tables and left for right-to-left tables. If omitted, the table will have 115 twentieths of a point (0.08 inches) of right padding unless a cell overrides the default.

Note: In the previous version of the standard, this element was right.

Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference § 17.4.11.

Attributes:

The attributes for the above table cell margin elements are:

AttributeDescription
wSpecifies the value of the width of the margin. If omitted, the value is assumed to be 0;
typeSpecifies the units of the width (w) property. Possible values are:
  • dxa - Specifies that the value is in twentieths of a point (1/1440 of an inch).
  • nil - Specifies a value of zero

If the attribute is omitted, its value is assumed to be dxa.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值