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
Element | Description |
---|---|
top | Specifies 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. |
bottom | Specifies 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. |
start | Specifies 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. |
end | Specifies 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. |
The attributes for the above table cell margin elements are:
Attribute | Description |
---|---|
w | Specifies the value of the width of the margin. If omitted, the value is assumed to be 0; |
type | Specifies the units of the width (w) property. Possible values are:
If the attribute is omitted, its value is assumed to be dxa. |