• python在excel中批量写入图片


    先手动生成一个excel文档,准备好图片文件夹的路径,使用以下代码将图片逐一插入excel。

    注意:生成的图片是悬浮在excel中。若要把图片都嵌入excel中,请看下一篇博客。

    from PIL import Image
    import os
    import xlwings as xw
    # app=xw.App(visible=True,add_book=False)
    
    wb = xw.Book(r'D:XXXX.xlsx')
    
    #居中 插入
    import os
    sht = wb.sheets['sheet1']
    
    def write_pic(cell,filename):
        
        print(path)
        img = Image.open(filename).convert("RGB")
        print(img.size)
        w, h = img.size
        x_s = 250 # 设置宽
        y_s = h * x_s / w  #  等比例设置高
        sht.pictures.add(filename, left=sht.range(cell).left, top=sht.range(cell).top, width=x_s, height=y_s)
    
    
    if __name__ == '__main__':
        index = 0
        path = r'D:XXX'
        for root,dirs,files in os.walk(path):
            for name in files:
                index = index +1
                filename = root+'\'+name
                cell="A"+str(index+2)
                try:
                    write_pic(cell,filename)
                except:
                    print("没有找到图片")
                #break
        wb.save()
  • 相关阅读:
    抱歉
    The area
    sort
    Problem D
    Problem B
    错排
    第8集 仪表开关电源照明原理图
    第7集 驱动与电气原理图绘制
    第6集 初步使用EPLAN部件库部件功能
    第5集 软件中建立项目结构
  • 原文地址:https://www.cnblogs.com/chenyaling/p/15523479.html
Copyright © 2020-2023  润新知