1.插入表格后,目前python-docx没有官方方法将单元格边距设置成自己想要的距离。
2.下面代码可实现调整单元格边距
from docx.table import _Celldef set_cell_margins(cell: _Cell, **kwargs):"""cell: actual cell instance you want to modifyusage: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: /WPtableCellMargins.php"""tc = cell._tctcPr = 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可点击/WPtableCellMargins.php
Elements:
Attributes:
The attributes for the above table cell margin elements are: