python 向excel 插入图片

这是工作中一个真实的需求。
要做gt excel 表,表中要插入图片。

1.要把图片resize 基本相同的大小。

2.通过一下脚本插入图片到excel

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import xlsxwriter

 
book = xlsxwriter.Workbook('C:/Users/micha/Desktop/测试/pict.xlsx') #新建一个 pict 的excel 表
sheet = book.add_worksheet('GT') #创建一个sheet 
sheet.set_column('A:C', 12)  #可以一次设置excel多列的宽度


for i in range(8):           #按行插入
    sheet.set_row(i, 85)     #设置行高(貌似一次只能设置一行,不能一次设置多行,具体没找到。暂时一行一行设置)
    file  = r"C:\Users\micha\Desktop\mask_test\mask_head_forward\out\000{}.png".format(i+1)   #要插入的图片
    des = '{}{}'.format('A', i+1)  #插入的excel中地址,入A0,B3
    sheet.insert_image(des,file)
 

book.close()
 
 
print("done!")


猜你喜欢

转载自www.cnblogs.com/michaelcjl/p/12525070.html