Python从Excel中按行提取图片的实现方法

 更新时间:2026年05月15日 08:27:10   作者:RumIV  
在Excel表格中批量导出图片,并按行首字段命名保存,是许多办公场景的刚需,本文提供两个完整可运行的Python版本,两个版本均实现了从Excel中按行提取图片,需要的朋友可以参考下

前言

在Excel表格中批量导出图片,并按行首字段命名保存,是许多办公场景的刚需。本文提供两个完整可运行的Python版本:

  • 版本一:保持图片原始格式(PNG、JPG等各自保留)
  • 版本二:统一将所有图片转换为JPG格式(节省空间、格式统一)

两个版本均实现了:按工作表分文件夹 → 根据图片所在行的A列值命名 → 自动处理重名和非法字符。

一、版本一:保持原始格式

该版本完全保留图片原始格式(.png / .jpg / .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.save(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格式(背景自动填充白色以处理透明通道),可设置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.save(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/

四、使用步骤(通用)

安装依赖

pip install openpyxl pillow

准备Excel文件

  • 确保图片是“嵌入”到单元格中的(而非浮动链接)。
  • 每个含图片的行,A列 必须有值,该值将作为图片文件名。

选择版本并修改路径
将代码末尾的 excel_path = 'cards.xlsx' 改为实际文件路径,可修改 output_dir

运行脚本

python extract_images.py   # 或 extract_as_jpg.py

结果
在输出目录下会按工作表名称创建子文件夹,图片按 A列值 命名存放。

五、注意事项

  • 图片定位:脚本依赖 openpyxl 的图片锚点属性,对于合并单元格或跨行图片可能定位不准,建议将图片完整置于某一行内。
  • 命名冲突:同一工作表中不同行的 A列值相同时,会自动添加 _1_2 后缀。
  • 非法字符:文件名中的 / \ : * ? " < > | 会被自动去除。
  • JPG 版本:原图为 GIF 动图时,只会提取第一帧并转为 JPG;原图为 CMYK 模式会自动转为 RGB。

六、扩展建议

  • 修改命名列:将 column=1 改为 column=2 即可使用B列命名。
  • 批量处理多个Excel:可在外层增加循环,依次调用函数。
  • 支持更多输出格式:在版本二中将 'JPEG' 改为 'PNG' 即可输出 PNG,但体积较大。
  • 保留透明通道转PNG:若希望统一为 PNG 并保留透明,可将版本二的转换代码去掉,直接保存为 PNG。

七、总结

本文提供了两个完整、可直接运行的 Python 脚本,分别满足“原格式保留”和“统一转 JPG”的常见需求。你只需要替换文件路径即可运行,大大提升从 Excel 批量导出图片的效率。选择适合自己的版本,开始自动化办公吧!

以上就是Python从Excel中按行提取图片的方法实现的详细内容,更多关于Python Excel按行提取图片的资料请关注脚本之家其它相关文章!

相关文章

  • 详解从Django Allauth中进行登录改造小结

    详解从Django Allauth中进行登录改造小结

    这篇文章主要介绍了从 Django Allauth 中进行登录改造小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • python中将字典形式的数据循环插入Excel

    python中将字典形式的数据循环插入Excel

    这篇文章主要介绍了python中将字典形式的数据循环插入Excel的方法,需要的朋友可以参考下
    2018-01-01
  • 基于Python打造高颜值软件卸载工具

    基于Python打造高颜值软件卸载工具

    在Windows系统中,自带的"添加/删除程序"功能一直饱受诟病,加载慢,功能弱,残留多,所以我们来使用Python打造一个高颜值软件的卸载工具吧
    2025-05-05
  • 10个的常用PyCharm插件(小结)

    10个的常用PyCharm插件(小结)

    本文主要介绍了10个的常用PyCharm插件,包括MaterialThemeUILite、中文语言包、Statistic、JsonParser等,帮助你在提升开发效率和视觉体验,感兴趣的可以了解一下
    2024-11-11
  • python 监控某个进程内存的情况问题

    python 监控某个进程内存的情况问题

    这篇文章主要介绍了python 监控某个进程内存的情况问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • pandas中concatenate和combine_first的用法详解

    pandas中concatenate和combine_first的用法详解

    本文主要介绍了pandas中concatenate和combine_first的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • python目标检测IOU的概念与示例

    python目标检测IOU的概念与示例

    这篇文章主要为大家介绍了python目标检测IOU的概念与示例实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Django模型中字段属性choice使用说明

    Django模型中字段属性choice使用说明

    这篇文章主要介绍了Django模型中字段属性choice使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Python pip替换为阿里源的方法步骤

    Python pip替换为阿里源的方法步骤

    这篇文章主要介绍了Python pip替换为阿里源的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Python采集王者最低战力信息实战示例

    Python采集王者最低战力信息实战示例

    这篇文章主要为大家介绍了Python采集王者最低战力信息实战示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04

最新评论