python-pptx 学习笔记 Tables

Tables API

#1.添加表格
#add_table方法得到shape对象,而不是table本身
from pptx import Presentation
from pptx.util import Inches

# ---create presentation with 1 slide---
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

# ---add table to slide---
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
shape = slide.shapes.add_table(3, 3, x, y, cx, cy)

shape
<pptx.shapes.graphfrm.GraphicFrame object at 0x1022816d0>、
#判断是否是表格
shape.has_table
True
table = shape.table

#2.访问单元格
#通过行列访问
cell = table.cell(0, 0)
cell.text

#3.合并单元格
cell = table.cell(0, 0)
other_cell = table.cell(1, 1)
cell.is_merge_origin
#False
cell.merge(other_cell)
cell.is_merge_origin
#True
cell.is_spanned
#False
other_cell.is_spanned
#True
table.cell(0, 1).is_spanned
#True

#4.取消合并单元格
cell = table.cell(0, 0)
cell.is_merge_origin
#True
cell.split()
cell.is_merge_origin
#False
table.cell(0, 1).is_spanned
#False

posted on 2022-05-26 15:32  朝朝暮Mu  阅读(393)  评论(0)    收藏  举报