Shape对象有一个text_frame属性可以返回该形状对应的文本框,即获取TextFrame对象。但是我们知道,并非所有的形状都有文本框,所以在获取Shape对象的TextFrame对象之前,可以先调用一下Shape对象的has_text_frame属性,如果该Shape对象没有文本框,则会返回False,代码如下:
from pptx import Presentation
ppt = Presentation()
slide = ppt.slides.add_slide(ppt.slide_layouts[0])
for shape in slide.shapes:
if not shape.has_text_frame:
continue
text_frame = shape.text_frame
print(type(text_frame))
# 输出:
# <class 'pptx.text.text.TextFrame'>
# <class 'pptx.text.text.TextFrame'>