首页 > 文章列表 > Python如何读取excel中的图片

Python如何读取excel中的图片

Python读取excel图片
299 2022-08-07

学会用Python提取word图片的小伙伴,今天又来学提取excel图片的方法啦。本期文章将通过python的包来提取,对比以往的代码更加简洁方便。

一、环境准备:

python3

pillow

pip install pillow

pypiwin32

pip install pypiwin32

二、操作代码

from PIL import ImageGrab
import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(r'C:Usersfile.xlsx')

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

三、注意事项

有些xlsx文件可能读取不了,试试换成xls格式

程序运行前不可以有其他程序打开excel文件

今天读取excel图片的方法比较简单,下期还有读取pdf图片的方法,大家不要错过哦~

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)