python-docx的源码如下:
def merge(self, other_cell):
"""
Return a merged cell created by spanning the rectangular region
having this cell and *other_cell* as diagonal corners. Raises
|InvalidSpanError| if the cells do not define a rectangular region.
"""
tc, tc_2 = self._tc, other_cell._tc
merged_tc = tc.merge(tc_2)
return _Cell(merged_tc, self._parent)
合并方法:
1.取出需要合并单元格的左上角那个单元格
2.通过merge方法,将需要合并单元格的右下角那个单元格作为参数传入
示例:
从表格的第一列合并到最后一列
cells = table.add_row().cells
cells[0].merge(cells[-1])