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

Shape对象有一个text_frame属性可以返回该形状对应的文本框,即获取TextFrame对象。但是我们知道,并非所有的形状都有文本框,所以在获取Shape对象的TextFrame对象之前,可以先调用一下Shape对象的has_text_frame属性,如果该Shape对象没有文本框,则会返回False,代码如下:

  1. from pptx import Presentation
  2. ppt = Presentation()
  3. slide = ppt.slides.add_slide(ppt.slide_layouts[0])
  4. for shape in slide.shapes:
  5. if not shape.has_text_frame:
  6. continue
  7. text_frame = shape.text_frame
  8. print(type(text_frame))
  9. # 输出:
  10. # <class 'pptx.text.text.TextFrame'>
  11. # <class 'pptx.text.text.TextFrame'>

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