要在Python中读取和解析PPT文档,可以使用第三方库`python-pptx`。下面是一个基本的例子,演示如何打开一个PPTX文档并获取每个幻灯片的标题和正文文本:
```
from pptx import Presentation
# 打开PPTX文件并创建Presentation对象
ppt = Presentation('example.pptx')
# 遍历每个幻灯片
for slide in ppt.slides:
# 获取幻灯片标题和正文文本
title = slide.shapes.title.text
body = '\n'.join([shape.text for shape in slide.shapes if not shape.has_text_frame or shape.text_frame.text.strip()])
# 输出标题和正文文本
print('Title: %s' % title)
print('Body: %s' % body)
```
注意需要安装`python-pptx`库,可以使用以下命令进行安装:
```
pip install python-pptx
```