You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按Excel工作表名称自动分离各工作表中的图片?

按工作表分类提取Excel中的图片

问题背景

需要从包含多工作表的Excel文件中提取图片,并按工作表名称自动分类到对应文件夹。尝试过两种方法但存在问题:

  • 将Excel改后缀为zip提取media文件夹的图片,所有图片混在一起,无法对应到工作表
  • 使用openpyxl提取,但因为图片非插入式,无提取结果

可行解决方案

方法1:VBA脚本(Windows环境)

该方法可遍历所有工作表,导出其中所有类型的图片(包括浮动式、非插入式)到对应文件夹,无需额外工具。

操作步骤:

  1. 打开目标Excel文件,按下Alt+F11打开VBA编辑器
  2. 右键左侧工程面板 → 插入 → 模块
  3. 粘贴以下代码:
Sub ExportImagesBySheet()
    Dim ws As Worksheet
    Dim shp As Shape
    Dim savePath As String
    Dim folderPath As String
    
    ' 根保存目录,可自行修改
    savePath = ThisWorkbook.Path & "\Excel图片提取\"
    
    For Each ws In ThisWorkbook.Worksheets
        ' 创建工作表对应文件夹
        folderPath = savePath & ws.Name & "\"
        If Dir(folderPath, vbDirectory) = "" Then
            MkDir folderPath
        End If
        
        ' 遍历工作表内所有形状,筛选图片类型
        For Each shp In ws.Shapes
            If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Then
                shp.CopyPicture
                ' 通过临时工作表导出图片
                With CreateObject("Excel.Sheet")
                    .Paste
                    .Shapes(1).Export folderPath & shp.Name & ".png"
                    .Close False
                End With
            End If
        Next shp
    Next ws
    
    MsgBox "图片提取完成!"
End Sub
  1. 按下F5运行脚本,图片会保存到Excel同目录下的Excel图片提取文件夹,每个工作表对应一个子文件夹。

方法2:Python + win32com(Windows环境)

如果习惯用Python,可通过调用Excel的COM接口实现相同效果:

  1. 先安装依赖:
pip install pywin32
  1. 运行以下代码(替换目标Excel路径):
import os
import win32com.client as win32

def export_images_by_sheet(excel_path):
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    excel.Visible = False
    workbook = excel.Workbooks.Open(excel_path)
    
    root_save_path = os.path.join(os.path.dirname(excel_path), "Excel图片提取")
    os.makedirs(root_save_path, exist_ok=True)
    
    for sheet in workbook.Worksheets:
        sheet_folder = os.path.join(root_save_path, sheet.Name)
        os.makedirs(sheet_folder, exist_ok=True)
        
        for shape in sheet.Shapes:
            ' 筛选图片类型(msoPicture=13,msoLinkedPicture=14)
            if shape.Type in (13, 14):
                shape.CopyPicture()
                temp_wb = excel.Workbooks.Add()
                temp_ws = temp_wb.Worksheets(1)
                temp_ws.Paste()
                temp_shape = temp_ws.Shapes(1)
                
                ' 处理重名图片
                img_name = f"{shape.Name}.png"
                img_path = os.path.join(sheet_folder, img_name)
                counter = 1
                while os.path.exists(img_path):
                    img_name = f"{shape.Name}_{counter}.png"
                    img_path = os.path.join(sheet_folder, img_name)
                    counter += 1
                
                temp_shape.Export(img_path)
                temp_wb.Close(SaveChanges=False)
    
    workbook.Close(SaveChanges=False)
    excel.Quit()
    print("图片提取完成!")

# 替换为你的Excel文件路径
export_images_by_sheet(r"C:\你的文件路径\target.xlsx")

注意事项

  • 以上两种方法均依赖Microsoft Excel客户端,需在Windows系统中安装
  • 若需提取工作表背景图,可在代码中额外判断处理(VBA中可通过ws.BackgroundPicture获取)
  • Mac系统用户可尝试使用appscript库替代win32com,或通过LibreOffice宏实现类似功能

内容的提问来源于stack exchange,提问作者How to be 6

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 10:25:29