Python获取excel中的图片文件

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')  # 创建一个支持excel的应用程序
    workbook = excel.Workbooks.Open(excel_path)  # 打开excel

    image_path = []
    for sheet in workbook.Worksheets:
        for i, shape in enumerate(sheet.Shapes):
            # 获取图表文件
            # 判断是否符合括号中的值
            # if shape.Name.startswith('Chart') or shape.Name.startswith('Picture') or shape.Name.startswith('图表'):
            if shape.Name.startswith(shape.Name.split(" ")[0]):  # 万能方法,可能会导出奇怪的东西
                print('正在保存图片:{}'.format(shape.Name))
                shape.Copy()
                image = ImageGrab.grabclipboard()  # 抓取当前剪贴板的快照
                # image.convert('RGB') 通道转换,默认是rgba
                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()    # 关闭excel
    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)
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值