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

标签 python python-docx

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

最佳答案

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

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

关于python - 如何使用python docx在ms word中设置表格的单元格边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060431/

相关文章:

python - 为什么 CTRL-C 没有被捕获并且 signal_handler 被调用?

python - 将Word文件与python-docx合并并添加分页符

Python 3 : How do I save a docx file to a specific directory?

python - 如何使用 python-docx 添加页面边框

python - 使用 Python 从 Word (.docx) 中提取表格标题

python - 从字典中获取最大值

Python Seaborn : Plot multiple distplot in Facetgrid

python - 如何用 sympy 绘制 x==2.5(垂直线)

python - 在终端中更改 Python 的路径

python - 在 python3.3 中导入 docx 时出现错误 ImportError : No module named 'exceptions'