本文目录导读:

我来帮你编写一个提取PPT中图片的Python脚本,这里提供两种方案:
使用python-pptx库(推荐)
安装依赖
pip install python-pptx
完整脚本代码
import os
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import argparse
from pathlib import Path
def extract_images_from_pptx(pptx_path, output_dir=None):
"""
从PPT文件中提取所有图片
Args:
pptx_path: PPT文件路径
output_dir: 输出目录(可选,默认为PPT所在目录的images文件夹)
"""
# 检查文件是否存在
if not os.path.exists(pptx_path):
print(f"错误:文件 {pptx_path} 不存在")
return
# 设置输出目录
pptx_name = Path(pptx_path).stem
if output_dir is None:
output_dir = os.path.join(os.path.dirname(pptx_path), f"{pptx_name}_images")
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 打开PPT文件
prs = Presentation(pptx_path)
image_count = 0
print(f"正在处理文件: {pptx_path}")
print(f"共 {len(prs.slides)} 张幻灯片")
print("-" * 50)
# 遍历每张幻灯片
for slide_num, slide in enumerate(prs.slides, 1):
print(f"处理第 {slide_num} 张幻灯片...")
# 遍历幻灯片中的所有形状
for shape_num, shape in enumerate(slide.shapes, 1):
try:
# 检查是否为图片
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
image = shape.image
# 获取图片格式
content_type = image.content_type
ext = content_type.split('/')[-1]
if ext == 'jpeg':
ext = 'jpg'
elif ext == 'x-ms-bmp':
ext = 'bmp'
elif ext == 'x-emf':
ext = 'emf'
# 保存图片
image_filename = f"slide{slide_num:03d}_image{shape_num:03d}.{ext}"
image_path = os.path.join(output_dir, image_filename)
with open(image_path, 'wb') as f:
f.write(image.blob)
image_count += 1
print(f" ✓ 保存图片: {image_filename}")
except Exception as e:
print(f" ✗ 处理形状 {shape_num} 时出错: {e}")
print("-" * 50)
print(f"提取完成!共提取 {image_count} 张图片")
print(f"图片保存在: {output_dir}")
return image_count
def main():
# 命令行参数解析
parser = argparse.ArgumentParser(description='从PPT文件中提取图片')
parser.add_argument('pptx_file', help='PPT文件路径')
parser.add_argument('-o', '--output', help='输出目录(可选)')
args = parser.parse_args()
# 执行提取
extract_images_from_pptx(args.pptx_file, args.output)
if __name__ == "__main__":
main()
使用方法
# 基本用法
python extract_ppt_images.py example.pptx
# 指定输出目录
python extract_ppt_images.py example.pptx -o ./my_images
# 或在Python代码中调用
from extract_ppt_images import extract_images_from_pptx
extract_images_from_pptx("example.pptx", "output_folder")
使用win32com(Windows Only)
如果你需要提取嵌入在形状或图表中的图片,可以使用这个方案:
import os
from pathlib import Path
import win32com.client
from PIL import Image
import io
def extract_images_win32(pptx_path, output_dir=None):
"""
使用win32com提取PPT中的图片(Windows only)
"""
if not os.path.exists(pptx_path):
print(f"错误:文件 {pptx_path} 不存在")
return
# 创建输出目录
pptx_name = Path(pptx_path).stem
if output_dir is None:
output_dir = os.path.join(os.path.dirname(pptx_path), f"{pptx_name}_images_win32")
os.makedirs(output_dir, exist_ok=True)
# 启动PowerPoint应用程序
powerpoint = win32com.client.Dispatch("PowerPoint.Application")
powerpoint.Visible = False
try:
# 打开演示文稿
presentation = powerpoint.Presentations.Open(os.path.abspath(pptx_path))
image_count = 0
# 遍历所有幻灯片
for slide_idx, slide in enumerate(presentation.Slides, 1):
print(f"处理第 {slide_idx} 张幻灯片...")
# 遍历所有形状
for shape_idx, shape in enumerate(slide.Shapes, 1):
try:
# 检查是否为图片
if shape.Type == 13: # msoPicture
# 导出为临时文件
temp_file = os.path.join(output_dir, f"temp_img.png")
shape.Export(temp_file, ppShapeFormatPNG)
# 重命名为有意义的名称
new_name = f"slide{slide_idx:03d}_shape{shape_idx:03d}.png"
new_path = os.path.join(output_dir, new_name)
if os.path.exists(temp_file):
if os.path.exists(new_path):
os.remove(new_path)
os.rename(temp_file, new_path)
image_count += 1
print(f" ✓ 保存图片: {new_name}")
except Exception as e:
print(f" ✗ 处理形状时出错: {e}")
print(f"\n提取完成!共提取 {image_count} 张图片")
print(f"图片保存在: {output_dir}")
finally:
# 关闭演示文稿和PowerPoint
presentation.Close()
powerpoint.Quit()
if __name__ == "__main__":
extract_images_win32("example.pptx")
高级功能:按图片尺寸过滤
如果你只想提取特定尺寸的图片:
def extract_images_with_filter(pptx_path, output_dir=None, min_size=(100, 100)):
"""
提取并过滤图片
"""
from pptx.util import Inches, Pt
# ... 前面的代码 ...
for slide_num, slide in enumerate(prs.slides, 1):
for shape_num, shape in enumerate(slide.shapes, 1):
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
# 获取图片尺寸
width = shape.width
height = shape.height
# 过滤小图片
if width >= Inches(min_size[0]) and height >= Inches(min_size[1]):
# 保存图片
# ... 保存代码 ...
注意事项
- 文件格式:脚本只支持
.pptx格式,不支持老的.ppt格式 - 图片格式:脚本会保持原始图片格式
- 批量处理:你可以修改脚本支持批量处理多个PPT文件
- 权限问题:确保对文件有读取权限
需要我帮你扩展某个功能吗?比如批量处理多个文件或添加GUI界面?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播