如何利用python向excel表格里插入图片xlsxwriter实现

该代码示例展示了如何通过Python的xlsxwriter库将一个目录下多个文件夹中的商品图片信息整理成Excel表格,每个文件夹的图片按大小排序,并在Excel中显示图片。首先遍历文件夹找到最大图片,然后将图片路径和相关信息写入Excel,并在单元格中插入图片。
摘要由CSDN通过智能技术生成

需求:一个目录下多个文件夹,每个文件夹里有多个商品图片,如何对这些数据可视化?
解决方案:建立excel表格,将文件夹里的图片按文件夹名进行展示

import os
import xlsxwriter
from operator import itemgetter
folder = '/data2/library_2403/' 

#定义一个获取文件夹里最大的照片
def find_img_path(sku_folder):
    images = []
    for file in os.listdir(sku_folder):
        if file.endswith('.jpg'):
            path = os.path.join(sku_folder, file)
            size = os.path.getsize(path)
            images.append((file, size))
    images = sorted(images, key=itemgetter(1), reverse=True)
    img_path=os.path.join(sku_folder, images[0][0])
    return img_path
#将所有的图片放在一个列表里
img_path_all=[]
for filename in os.listdir(folder):
    sku_folder=os.path.join(folder,filename)
    if sku_folder.split("/")[-1]=="facebank.pth" or sku_folder.split("/")[-1]=="names.npy":
        continue
    img_path_all.append(find_img_path(sku_folder))
wb = xlsxwriter.Workbook('/data2/2403sku_all_0201_20230201_NEW.xls')   #打开excel
pictureSheet = wb.add_worksheet("Sheet2") #添加一个sheet
pictureSheet.set_column('C:C', 16) #设置行高
for i in range(0,len(img_path_all)):
    print(img_path_all[i].split("/")[-2],dict1[img_path_all[i].split("/")[-2]])  
    cell = 'A%d' % i
    cell2 = 'B%d' % i
    cell3 = 'C%d' % i
    pictureSheet.set_row(i, 95) #设置列宽
    pictureSheet.write(cell, img_path_all[i].split("/")[-2])
    pictureSheet.write(cell2, dict1[img_path_all[i].split("/")[-2]])
    pictureSheet.insert_image(cell3,img_path_all[i],{'x_offset': 5, 'y_offset': 1,'x_scale': 0.5, 'y_scale': 0.5})  # 指定单元格,x、y为缩放比例
wb.close()  

本来使用openpyxl模块
但运行时报错zipfile.BadZipFile: File is not a zip file
查看网上解决方案:
1.有可能是因为openpyxl与pandas的版本兼容发生了问题,解决方法是将pd.excelwriter(path,engine=‘openpyxl’)改成pd.excelwriter(path,engine=‘openpyxl’,mode=‘a’)
2.其他解决方案
https://www.cnblogs.com/qingyuu/p/10642249.html
太麻烦了最终选择 xlsxwriter库来解决问题
最终效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值