用python如何绘制表格不同颜色的excel
作者:广大菜鸟 发布时间:2022-08-09 22:05:11
标签: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])
需求代码:
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)
颜色没对上,意思差不多就行了
来源:https://blog.csdn.net/weixin_44001521/article/details/121141741
0
投稿
猜你喜欢
- 地址:https://youzan.github.io/vant/#/zh-CN/intro一.引入Vant组件库1.首先运行 npm in
- js 代码中经常会碰到 undefined 这种错误,下面本文分享一下为什么会发生这种错误以及如何处理这种错误,js 中如果通过 var 声
- 这片文章大体概括了一些设计网页中的大问题。希望能给你做下一个网页时给予一些儿启发。1、记住:你能掌控的时间是有限的我仅仅用4秒钟就能浏览完平
- 外联接。外联接可以是左向外联接、右向外联接或完整外部联接。 在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:LEFT J
- PHP getDocNamespaces() 函数实例返回 XML 文档的根节点中声明的命名空间:<?php $xml=<<
- 如下所示:a[:, np.newaxis] # 给a最外层中括号中的每一个元素加[]a[np.newaxis, :] # 给a最外层中括号中
- 说明和代码如下:<%@ language = vbscript%><% 
- 1.字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66
- 什么是1433端口 1433端口,是SQL Server默认的端口,SQL Server服务使用两个端口:TCP-1433、UDP-1434
- 通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送,分享给大家供大家参考,具体内容如下此方法是通过模拟登录微信公共
- 客户端: <%@ Page Language="C#" AutoEventWireup="true&qu
- 经常在网上冲浪的朋友是否曾注意到有些网站的鼠标不是规则的斜向上箭头的形状,而是"十"
- 呵,以前也没考虑过这方面的东西,现在写的代码越来越多,越来越复杂,如果再不把不用的变量及时释放掉,到时肯定会出问题。今天无意中在无忧Q群里看
- Css Reset是什么? 有些同行叫 "css复位",有些可能叫 "默认css".....相信看完
- system默认:managersys默认:change_on_install使用SQL Plus登录数据库时,system使用密码mana
- 本文主要介绍 SQLServerExpress2008不用第三方工具调试T-SQL语句,经过本文的介绍,用SQLSERVER2008 Man
- 数据库(DataBase,DB)是一个长期存储在计算机内的、有组织的、有共享的、统一管理的数据集合。通俗地说,数据库就是一个按照数据结构来组
- javascript版 俄罗斯方块(Russian box)小游戏,喜欢的朋友可以玩玩。对源代码感兴趣的朋友也可以研究一下。玩法介绍:可以输
- php获取 checkbox复选框值的方法 <html xmlns="https://www.aspxhome.net/19
- 超酷的js图片轮换/轮播 渐变效果··来自腾讯刚刚在腾讯女性频道上看到一个很酷的图片渐变轮换效果·····于是乎····抠下来了···分享·