您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:用python如何绘制表格不同颜色的excel

51自学网 2022-02-21 10:49:26
  python
这篇教程用python如何绘制表格不同颜色的excel写得很实用,希望能帮到您。

需求:

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

先实现个差不多的

m = 5cols = 9rows = 4nums = [0, 1]array = [[-1 for _ in range(9)] for _ in range(4)]i, j = 0, 0t = 0index = -1while 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 Workbookfrom 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, 9colorIndex = -1block_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内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


python算法学习双曲嵌入论文代码实现数据集介绍
python算法学习双曲嵌入论文方法与代码解析说明
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。