python 提取pptx上的图片


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)

posted @ 2025-08-25 16:21  乐乐乐乐乐乐樂  阅读(7)  评论(0)    收藏  举报
jQuery火箭图标返回顶部代码