import os
from pptx import Presentation
def extract_images(pptx_path, output_dir):
# 加载PPTX文件
prs = Presentation(pptx_path)
# 确保输出目录存在
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 遍历每个幻灯片,使用enumerate获取索引
for slide_index, slide in enumerate(prs.slides, 1):
# 遍历幻灯片中的每个形状,使用enumerate获取索引
for shape_index, shape in enumerate(slide.shapes, 1):
# 检查形状是否为图片
if hasattr(shape, 'image') and shape.image is not None:
# 获取图片数据并保存
image = shape.image
image_path = os.path.join(output_dir, f"image_{slide_index}_{shape_index}.png")
with open(image_path, 'wb') as f:
f.write(image.blob) # 使用blob属性保存图片数据
print(f"Saved image to {image_path}")
if __name__ == '__main__':
pptx_path = './practice_files/de.pptx'
output_dir = './practice_files'
extract_images(pptx_path, output_dir)