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

在python-pptx中,表格也是一种形状,所以我们还是要借助SlideShapes对象,它有一个add_table()方法,该方法一共有六个参数,不用担心,这些参数都是很简单的,前面两个参数分别是行数和列数,中间两个参数分别是到达页面左边缘和上边缘的距离,最后两个参数是宽高。如此,我们可以先添加一个表格看看效果,代码如下:

  1. from pptx import Presentation
  2. from pptx.util import Cm
  3. ppt = Presentation()
  4. slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  5. row = 4
  6. column = 3
  7. left = top = Cm(3)
  8. width = Cm(20)
  9. height = Cm(10)
  10. graphic_frame = slide.shapes.add_table(row,column,top,left,
  11. width,height)
  12. print(type(graphic_frame))
  13. # 输出:<class 'pptx.shapes.graphfrm.GraphicFrame'>
  14. ppt.save("./ppt_ files/test.pptx")

结果如我们所料,执行上面的代码之后顺利在PPT中添加了一个4行3列的表格,这个简单的表格会有一个默认的样式,如图8-11所示。


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