python 如何创建出如下格式的

python 读取一个有数据的excel,里面的数据是19行9列
表格每列填充N个格子,不足的等待等待所有列填充玩成后,从剩余补足
表格填充颜色随意,要做到相邻颜色不同,上下颜色不同,
python 如何通过 pandas 或者其他模块创建一个这样的excel表,资源链接如下

img


excel文件下载链接 https://gitee.com/xxhaadyq/image/raw/master/picture/%E5%B7%A5%E4%BD%9C%E7%B0%BF1.xlsx


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)

img

img


获取像素16进制方法:

img

img

img