Python 获取excel中的图片文件
需要用到的库
- pip install pillow
- pip install pypiwin32
示例代码:
from PIL import ImageGrab
import win32com.client as win32
import os
def get_excel_image(excel_path, save_path):
"""
从excel中导出图片
:param excel_path: excel 路径
:param save_path: 图片保存路径
:return:image_path 文件路径
"""
excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(excel_path)
image_path = []
for sheet in workbook.Worksheets:
for i, shape in enumerate(sheet.Shapes):
if shape.Name.startswith(shape.Name.split(" ")[0]):
print('正在保存图片:{}'.format(shape.Name))
shape.Copy()
image = ImageGrab.grabclipboard()
image.convert('RGB').save(os.path.join(save_path, "{}.png".format(shape.Name)), 'jpeg')
image_path.append(os.path.join(save_path, "{}.png".format(shape.Name)))
else:
print(shape.Name)
print("{}不是图片".format(shape.Name))
excel.Quit()
return image_path
excel_path = r"G:\LP\job\Python\平时用\123 - 副本.xlsx"
save_path = r"G:\LP\job\Python\平时用\image"
image_path = get_excel_image(excel_path, save_path)
print(image_path)