from openpyxl import Workbook
from openpyxl.styles import PatternFill, Side, Border
# 仿照excel格式
# excel文件路径
file_path = 'C:/Users/Lenovo/Desktop/工作簿2.xlsx'
colors = ['000000', '44546A']
fills = [PatternFill("solid", fgColor=color) for color in colors]
workbook = Workbook()
sheet = workbook.create_sheet("Sheet1", 0)
rows, cols = 30, 9
colorIndex = 1
block_height, block_width = 5, 1
for i in range(int(rows / block_height)):
for j in range(int(cols / block_width)):
colorIndex = (colorIndex + 1) % 2
for p in range(block_height):
row = block_height * i + p
for q in range(block_width):
col = j * block_width + q
cell = sheet.cell(column=col + 1, row=row + 1)
cell.fill = fills[colorIndex]
cell.border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
workbook.save(file_path)
获取像素16进制方法: