Python怎样提取Excel中的图片

1、安装库

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pypiwin32 pillow

2、代码

# pip install pypiwin32 pillow
from PIL import ImageGrab
import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(r'd:/待处理excel.xlsx')

num = 1
for sheet in workbook.Worksheets:
    for i, shape in enumerate(sheet.Shapes):
        if shape.Name.startswith('Picture'):
            shape.Copy()
            image = ImageGrab.grabclipboard()
            image.convert('RGB').save(f'{num}.jpg', 'jpeg')
            num += 1
excel.Quit()

3、演示效果

file

file

file

Leave a Comment