如何使用python docx以ms字为单位设置表格的单元格边距

问题描述 投票:0回答:2

据我所知,你只能改变表的单元格宽度。

python python-docx
2个回答
1
投票

我使用这个片段来修改单元格的边距

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)

你可以在这里阅读更多关于openxml标签的信息http://officeopenxml.com/WPtableCellMargins.php


-1
投票

我将代码移植到pywin32。它提供了vba的全部功能。 python-docx不仅仅是基本任务的好处。

© www.soinside.com 2019 - 2024. All rights reserved.