您的位置:首页 >Python从Excel中按行提取图片的实现方法
发布于2026-05-20 阅读(0)
扫一扫,手机访问
在处理Excel文件时,批量导出其中的图片并按特定规则命名,是许多数据分析、物料管理和内容归档工作中的常见痛点。手动操作不仅繁琐,还极易出错。今天,我们就来分享两个“开箱即用”的Python脚本,帮你彻底解决这个问题。
这两个版本都实现了核心功能:按工作表自动创建文件夹,并根据图片所在行的A列内容来命名图片文件。同时,它们都内置了重名处理和非法字符过滤机制,确保运行稳定。二者的主要区别在于对图片格式的处理策略不同。
如果你需要严格保留图片的原始属性,比如PNG的透明背景、GIF的动态效果,或者不希望因二次压缩损失画质,那么这个版本就是为你准备的。它会原封不动地保存图片的原始格式。
import openpyxl
from PIL import Image as PILImage
import io
import os
def extract_images_from_excel(excel_path, output_dir=None):
"""
按行提取 Excel 中的图片,存储到以 Sheet 名称命名的文件夹下,
图片命名为该行第一个字段的值(即A列),保持原始格式。
"""
if output_dir is None:
output_dir = os.path.dirname(os.path.abspath(excel_path))
os.makedirs(output_dir, exist_ok=True)
wb = openpyxl.load_workbook(excel_path)
extracted_count = 0
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
print(f"处理工作表: {sheet_name}")
safe_sheet_name = "".join(
c for c in sheet_name if c not in r'\/:*?"<>|'
).strip()
sheet_dir = os.path.join(output_dir, safe_sheet_name)
os.makedirs(sheet_dir, exist_ok=True)
# 收集每行A列的值
row_first_cell = {}
for row in range(1, ws.max_row + 1):
cell_value = ws.cell(row=row, column=1).value
if cell_value is not None:
row_first_cell[row] = str(cell_value)
for image in ws._images:
anchor = image.anchor
if hasattr(anchor, '_from'):
row_num = anchor._from.row + 1
elif hasattr(anchor, 'row'):
row_num = anchor.row + 1
else:
print(f" ⚠️ 无法确定图片行号,跳过")
continue
first_cell_value = row_first_cell.get(row_num, None)
if first_cell_value is None:
print(f" ⚠️ 第{row_num}行第一个字段为空,跳过")
continue
safe_name = "".join(
c for c in first_cell_value if c not in r'\/:*?"<>|'
).strip()
if not safe_name:
safe_name = f"row_{row_num}"
img_data = image._data()
img = PILImage.open(io.BytesIO(img_data))
# 获取原始格式
img_format = img.format.lower() if img.format else 'png'
filename = f"{safe_name}.{img_format}"
filepath = os.path.join(sheet_dir, filename)
counter = 1
base_name = safe_name
while os.path.exists(filepath):
filename = f"{base_name}_{counter}.{img_format}"
filepath = os.path.join(sheet_dir, filename)
counter += 1
img.sa ve(filepath)
extracted_count += 1
print(f" ✅ 第{row_num}行 -> {safe_sheet_name}/{filename}")
print(f"\n? 完成!共提取 {extracted_count} 张图片,保存至: {output_dir}")
if __name__ == '__main__':
excel_path = 'cards.xlsx' # 修改为你的文件
extract_images_from_excel(excel_path, output_dir='images')
对于需要统一图片格式、减小文件总体积,或者后续处理流程只接受JPG的场景,这个版本会更合适。它会将所有图片统一转换为JPG格式,并自动处理透明背景(填充为白色),你还可以自由调节输出图片的质量。
import openpyxl
from PIL import Image as PILImage
import io
import os
def extract_images_as_jpg(excel_path, output_dir=None, jpg_quality=85):
"""
按行提取 Excel 中的图片,统一转换为 JPG 格式存储。
参数:
excel_path: Excel 文件路径
output_dir: 输出根目录
jpg_quality: JPG 压缩质量 (1-100),默认85
"""
if output_dir is None:
output_dir = os.path.dirname(os.path.abspath(excel_path))
os.makedirs(output_dir, exist_ok=True)
wb = openpyxl.load_workbook(excel_path)
extracted_count = 0
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
print(f"处理工作表: {sheet_name}")
safe_sheet_name = "".join(
c for c in sheet_name if c not in r'\/:*?"<>|'
).strip()
sheet_dir = os.path.join(output_dir, safe_sheet_name)
os.makedirs(sheet_dir, exist_ok=True)
# 收集每行A列的值
row_first_cell = {}
for row in range(1, ws.max_row + 1):
cell_value = ws.cell(row=row, column=1).value
if cell_value is not None:
row_first_cell[row] = str(cell_value)
for image in ws._images:
anchor = image.anchor
if hasattr(anchor, '_from'):
row_num = anchor._from.row + 1
elif hasattr(anchor, 'row'):
row_num = anchor.row + 1
else:
print(f" ⚠️ 无法确定图片行号,跳过")
continue
first_cell_value = row_first_cell.get(row_num, None)
if first_cell_value is None:
print(f" ⚠️ 第{row_num}行第一个字段为空,跳过")
continue
safe_name = "".join(
c for c in first_cell_value if c not in r'\/:*?"<>|'
).strip()
if not safe_name:
safe_name = f"row_{row_num}"
img_data = image._data()
img = PILImage.open(io.BytesIO(img_data))
# ----- 转换为 JPG 的关键代码 -----
# 若图片为 RGBA(带透明通道),需转换为 RGB(白色背景)
if img.mode in ('RGBA', 'LA', 'P'):
# 创建白色背景
background = PILImage.new('RGB', img.size, (255, 255, 255))
# 将原图粘贴到背景(使用透明通道作为掩码)
if img.mode == 'P':
img = img.convert('RGBA')
background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
img = background
elif img.mode != 'RGB':
img = img.convert('RGB')
# -----------------------------
filename = f"{safe_name}.jpg"
filepath = os.path.join(sheet_dir, filename)
counter = 1
base_name = safe_name
while os.path.exists(filepath):
filename = f"{base_name}_{counter}.jpg"
filepath = os.path.join(sheet_dir, filename)
counter += 1
# 保存为 JPG,可指定质量
img.sa ve(filepath, 'JPEG', quality=jpg_quality)
extracted_count += 1
print(f" ✅ 第{row_num}行 -> {safe_sheet_name}/{filename} (JPG质量={jpg_quality})")
print(f"\n? 完成!共提取 {extracted_count} 张图片,已统一为JPG格式,保存至: {output_dir}")
if __name__ == '__main__':
excel_path = 'cards.xlsx' # 修改为你的文件
extract_images_as_jpg(excel_path, output_dir='images_jpg', jpg_quality=85)
为了让你能快速决策,这里将两个方案的核心差异对比如下:
| 特性 | 版本一(原格式) | 版本二(统一JPG) |
|---|---|---|
| 输出扩展名 | .png / .jpg / .gif 等原样保留 | 统一为 .jpg |
| 透明背景处理 | 保留原图(PNG透明通道) | 白色背景填充 |
| 压缩控制 | 无(原样保存) | 可调节 quality 参数 |
| 适用场景 | 需要保留原始质量/透明效果 | 统一浏览、减小体积 |
| 输出文件夹 | images/ | images_jpg/ |
无论选择哪个版本,准备工作都是类似的:
1. 安装依赖
首先,确保你的Python环境已安装必要的库:
pip install openpyxl pillow
2. 准备Excel文件
3. 选择版本并修改路径
将脚本末尾的 excel_path = 'cards.xlsx' 替换成你实际Excel文件的路径。你也可以根据需要修改 output_dir 参数来指定输出目录。
4. 运行脚本
python extract_images.py # 或 extract_as_jpg.py
5. 查看结果
脚本运行后,会在你指定的输出目录下,为每个工作表创建一个同名的子文件夹,所有图片都会按A列的值命名并保存在对应的文件夹里。
_1、_2 这样的后缀,避免覆盖。/ \ : * ? " < > |)会被自动过滤掉。这两个脚本提供了很好的基础,你可以根据实际需求轻松调整:
column=1 的地方,将其改为 column=2 即可。'JPEG' 参数改为 'PNG',就可以统一输出为PNG格式,不过文件体积会比较大。通过以上两个Python脚本,我们解决了从Excel中批量、按规则导出图片的自动化需求。一个忠于原貌,一个统一格式,它们覆盖了绝大多数办公场景。你只需要复制代码,修改文件路径,就能将繁琐的手动操作转化为瞬间完成的自动化流程。选择适合你需求的那个版本,开始体验高效办公吧。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8