既然有合并单元格的操作,就肯定有取消合并单元格的操作,也就是把已合并的单元格拆分为原来的样子,这操作很简单,只要调用_Cell对象的split()方法就行,不需要传入任何参数。但是要注意的是,只有合并过其他单元格的_Cell对象才有取消合并的资格,所以我们在取消合并之前最好判断一下is_merge_origin属性是否为True,否则可能会引发异常,代码如下:
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
ppt = Presentation("./ppt_ files/test.pptx")
for slide in ppt.slides:
for shape in slide.shapes:
if shape.shape_type != MSO_SHAPE_TYPE.TABLE:
continue
table = shape.table
c1 = table.cell(0,1)
c2 = table.cell(2,2)
print(c1.is_merge_origin) # 输出:True
print(c2.is_spanned) # 输出:True
print(table.cell(1,1).is_spanned) # 输出:True
if c1.is_merge_origin:
c1.split()
print(c1.is_merge_origin) # 输出:False
print(c2.is_spanned) # 输出:False
print(table.cell(1,1).is_spanned) # 输出:False