当前位置:  首页>> 技术小册>> Python与办公-玩转PPT

既然有合并单元格的操作,就肯定有取消合并单元格的操作,也就是把已合并的单元格拆分为原来的样子,这操作很简单,只要调用_Cell对象的split()方法就行,不需要传入任何参数。但是要注意的是,只有合并过其他单元格的_Cell对象才有取消合并的资格,所以我们在取消合并之前最好判断一下is_merge_origin属性是否为True,否则可能会引发异常,代码如下:

  1. from pptx import Presentation
  2. from pptx.enum.shapes import MSO_SHAPE_TYPE
  3. ppt = Presentation("./ppt_ files/test.pptx")
  4. for slide in ppt.slides:
  5. for shape in slide.shapes:
  6. if shape.shape_type != MSO_SHAPE_TYPE.TABLE:
  7. continue
  8. table = shape.table
  9. c1 = table.cell(0,1)
  10. c2 = table.cell(2,2)
  11. print(c1.is_merge_origin) # 输出:True
  12. print(c2.is_spanned) # 输出:True
  13. print(table.cell(1,1).is_spanned) # 输出:True
  14. if c1.is_merge_origin:
  15. c1.split()
  16. print(c1.is_merge_origin) # 输出:False
  17. print(c2.is_spanned) # 输出:False
  18. print(table.cell(1,1).is_spanned) # 输出:False

该分类下的相关小册推荐: