学会用Python提取word图片的小伙伴,今天又来学提取excel图片的方法啦。本期文章将通过python的包来提取,对比以往的代码更加简洁方便。
一、环境准备:
python3
pillow
pipinstallpillow
pypiwin32
pipinstallpypiwin32
二、操作代码
fromPILimportImageGrab importwin32com.clientaswin32 excel=win32.gencache.EnsureDispatch('Excel.Application') workbook=excel.Workbooks.Open(r'C:\Users\file.xlsx') forsheetinworkbook.Worksheets: fori,shapeinenumerate(sheet.Shapes): ifshape.Name.startswith('Picture'): shape.Copy() image=ImageGrab.grabclipboard() image.save('{}.jpg'.format(i+1),'jpeg') excel.Quit()
三、注意事项
有些xlsx文件可能读取不了,试试换成xls格式
程序运行前不可以有其他程序打开excel文件
今天读取excel图片的方法比较简单,下期还有读取pdf图片的方法,大家不要错过哦~更多Python学习推荐:PyThon学习网教学中心。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)