from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import PatternFill,Color
from PIL import Image
#1.创建一个工作簿
workbook = Workbook()
#2.读取自带的工作表
worksheet = workbook.active
#3.打开图片
im=Image.open(r"C:\Users\Polyxena\Desktop\test\xiaohuangya.png")
im_witdth = im.size[0]
im_height = im.size[1]
pix = im.load()
#4…读取像素
for row in range(1,im_height):
for col in range(1,im_witdth):
cell = worksheet.cell(column=col ,row=row)
#读取每一个像素点
pixpoint = pix[col-1,row-1]
#颜色rgb,红绿蓝
pixColor = “FF%02X%02X%02X” % (pixpoint[0],pixpoint[1],pixpoint[2])
#颜色填充
fill = PatternFill(patternType=“solid”,fgColor=Color(rgb=pixColor))
cell.fill = fill
#设置单元格的高度
worksheet.row_dimensions[row].height = 6
#设置单元格的高度
for col in range(1,im_witdth):
worksheet.column_dimensions[get_column_letter(col)].width = 1
workbook.save(filename=‘xiaohuangya.xlsx’)
用excel填充图片
最新推荐文章于 2024-06-18 09:11:11 发布