python绘制表格不同颜色的excel

本文介绍了一个简单的S型数组赋值算法,并通过Python的openpyxl库实现了带有颜色填充和边框样式的Excel表格。该表格采用特定的颜色序列进行填充,并通过遍历实现了指定的样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:

需求简单:但是感觉最后那部分遍历有意思:S型数组赋值,考虑到下标,简单题

先实现个差不多的

m = 5
cols = 9
rows = 4
nums = [0, 1]
array = [[-1 for _ in range(9)] for _ in range(4)]
i, j = 0, 0
t = 0
index = -1
while t < cols * rows:
    if i % rows == 0 and i > 0:
        j += 1
        i -= 1
    if i < 0:
        j += 1
        i += 1
    # if t % m == 0:
    #     index = (index + 1) % len(nums)
    array[i][j] = t  # index
    if j % 2 == 0:  # 0,2,..2n 下
        i += 1
    else:  # 1,3, 2n+1 上
        i -= 1
    t += 1

for i in range(4):
    print(array[i])

需求代码: 

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Side, Border

# 仿照excel格式
# excel文件路径
file_path = 'C:/Users/Lenovo/Desktop/工作簿2.xlsx'

colors = ['000000', '44546A', 'CC00FF', '00008B']
colorsLen = len(colors)
fills = [PatternFill("solid", fgColor=color) for color in colors]
workbook = Workbook()
sheet = workbook.create_sheet("Sheet1", 0)
rows, cols = 19, 9
colorIndex = -1
block_height = 5

# 按行
for i in range(int(rows / block_height)):
    for j in range(cols):
        colorIndex = (colorIndex + 1) % colorsLen
        for p in range(block_height):
            row = block_height * i + p
            col = j 
            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'))

# 按列
if rows % block_height != 0:
    newRows = rows % block_height
    preRows = rows - rows % newRows - 1
    newCols = cols
    i, j = 0, 0
    t = 0
    while t < newCols * newRows:
        if i % newRows == 0 and i > 0:
            j += 1
            i -= 1
        if i < 0:
            j += 1
            i += 1
        if t % block_height == 0:
            colorIndex = (colorIndex + 1) % colorsLen
        cell = sheet.cell(column=j + 1, row=preRows + i + 1)
        cell.fill = fills[colorIndex]
        cell.border = Border(left=Side(style='thin'),
                             right=Side(style='thin'),
                             top=Side(style='thin'),
                             bottom=Side(style='thin'))
        if j % 2 == 0:  # 0,2,..2n 下
            i += 1
        else:  # 1,3, 2n+1 上
            i -= 1
        t += 1

workbook.save(file_path)

# 下面是学习读取的部分代码
# wb = openpyxl.load_workbook(file_path)
# sheet_name = 'Sheet1'
# sheet = wb.get_sheet_by_name(sheet_name)
# for r in range(1, sheet.max_row + 1):
#     for c in range(1, sheet.max_column + 1):
#         item = sheet.cell(row=r, column=c)
#         print(item, end=' ')
#     print()
# wb.save(file_path)

颜色没对上,意思差不多就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值