如何使用python docx在ms word中设置表格的单元格边距

时间:2018-06-27 10:26:15

标签: python python-docx

据我了解,您只能更改表格的单元格宽度。

2 个答案:

答案 0 :(得分:0)

我将代码移植到pywin32。它提供了vba的全部功能。 python-docx除了基本任务外,无能为力。

答案 1 :(得分:0)

我使用此代码段修改单元格的边距

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)

您可以在http://officeopenxml.com/WPtableCellMargins.php

处了解有关openxml标记的更多信息。