用PPT有时候需要把其实的大部分图片无压缩另存出来,手动右键太慢了,写了个python,优化了一下,可以根据图片的格式另存出对应后缀的图片,避免放入PS编辑不了。
注意如果是python3.9以上版本,要先修改pptx库中的compat/__init__.py文件。
一、主要代码
1、安装库
pip install python-pptx
2、核心代码,复制运行就可以,如果出错就看下一步
from pptx import Presentation
import os
from datetime import datetime
# 定义函数以保存图像
def save_image(image, image_folder, image_count):
# 获取当前时间并格式化
current_time = datetime.now().strftime('%Y%m%d%H%M%S')
# 获取图像格式
image_format = image.ext.split('.')[-1] if '.' in image.ext else 'jpg'
# 构建图像文件名
image_filename = os.path.join(image_folder, f'image_{current_time}_{image_count}.{image_format}')
# 保存图像
with open(image_filename, 'wb') as f:
f.write(image.blob)
# 定义提取图像的函数
def extract_images_from_pptx(pptx_path, output_folder):
# 打开PowerPoint文件
prs = Presentation(pptx_path)
# 确保输出文件夹存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
image_count = 0
# 遍历所有幻灯片和形状
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, 'image'):
# 保存图像
save_image(shape.image, output_folder, image_count)
image_count += 1
# 设置PowerPoint文件路径和输出文件夹路径
pptx_path = r'C:/Users/XXX.pptx' # 请替换为你的PowerPoint文件路径
output_folder = r'C:/Users/XXX/extracted_images' # 请替换为你想输出图片文件路径
# 提取并保存图像
extract_images_from_pptx(pptx_path, output_folder)
print(f'所有图片已保存到文件夹: {output_folder}')
二、python3.10及以上需要修改pptx库
1、找到 `pptx` 库安装的位置。你可以使用以下命令找到库的位置:
pip show python-pptx
这会显示类似以下的信息:
Name: python-pptx
Version: 0.6.21
Summary: Generate and manipulate Open XML PowerPoint (.pptx) files
Home-page: https://github.com/scanny/python-pptx
Author: Steve Canny
Author-email: [email protected]
License: MIT
Location: /path/to/python/site-packages
Requires: Pillow, lxml, XlsxWriter
Required-by:
```
`Location` 一行显示了库安装的位置。
2. 找到并编辑 `pptx/compat/__init__.py` 文件:
- 打开 `/path/to/python/site-packages/pptx/compat/__init__.py` 文件(路径根据上一步的 `Location`)。
将原来的代码
# encoding: utf-8
"""Provides Python 2/3 compatibility objects."""
import sys
import collections
try:
Container = collections.abc.Container
Mapping = collections.abc.Mapping
Sequence = collections.abc.Sequence
except AttributeError:
Container = collections.Container
Mapping = collections.Mapping
Sequence = collections.Sequence
if sys.version_info >= (3, 0):
from .python3 import ( # noqa
BytesIO,
is_integer,
is_string,
is_unicode,
to_unicode,
Unicode,
)
else:
from .python2 import ( # noqa
BytesIO,
is_integer,
is_string,
is_unicode,
to_unicode,
Unicode,
)
改为:
# encoding: utf-8
"""Provides Python 2/3 compatibility objects."""
import sys
try:
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc
try:
Container = collections_abc.Container
Mapping = collections_abc.Mapping
Sequence = collections_abc.Sequence
except AttributeError:
Container = collections_abc.Container
Mapping = collections_abc.Mapping
Sequence = collections_abc.Sequence
if sys.version_info >= (3, 0):
from .python3 import ( # noqa
BytesIO,
is_integer,
is_string,
is_unicode,
to_unicode,
Unicode,
)
else:
from .python2 import ( # noqa
BytesIO,
is_integer,
is_string,
is_unicode,
to_unicode,
Unicode,
)
保存,ok