欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用python如何绘制表格不同颜色的excel

程序员文章站 2022-06-24 09:43:56
目录需求:先实现个差不多的需求代码:总结需求:需求简单:但是感觉最后那部分遍历有意思:s型数组赋值,考虑到下标,简单题先实现个差不多的m = 5cols = 9rows = 4nums = [0, 1...

需求:

用python如何绘制表格不同颜色的excel

需求简单:但是感觉最后那部分遍历有意思: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])

用python如何绘制表格不同颜色的excel

需求代码:

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)

用python如何绘制表格不同颜色的excel

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

总结

到此这篇关于用python如何绘制表格不同颜色excel的文章就介绍到这了,更多相关python绘制不同颜色excel内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!