用python如何绘制表格不同颜色的excel
作者:广大菜鸟 时间:2022-08-09 22:05:11
需求:
需求简单:但是感觉最后那部分遍历有意思: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
标签:python,绘制,excel
0
投稿
猜你喜欢
Div即父容器不根据内容自适应高度的解决方法
2010-04-23 18:19:00
Pandas 同元素多列去重的实例
2023-02-09 21:03:32
MySQL创建用户和权限管理的方法
2024-01-21 12:08:58
字符集和字符编码(Charset & Encoding)
2023-08-24 16:37:44
python2.7 mayavi 安装图文教程(推荐)
2022-05-12 05:39:13
如何编写TOP10之类的排行榜?
2009-11-07 18:45:00
快速解决vue.js 模板和jinja 模板冲突的问题
2023-04-04 12:49:59
python计算程序开始到程序结束的运行时间和程序运行的CPU时间
2023-08-04 02:11:09
详解Python中的三器一闭
2023-05-30 15:08:16
浅谈web分析
2008-12-02 15:52:00
python异常处理try的实例小结
2022-01-25 06:06:51
成为一个顶级设计师的第二准则
2008-04-01 09:41:00
python 通过logging写入日志到文件和控制台的实例
2021-04-26 00:04:38
oracle11g卸载完整图文教程
2024-01-26 18:23:42
python中字典按键或键值排序的实现代码
2023-07-22 00:26:49
C# Ado.net实现读取SQLServer数据库存储过程列表及参数信息示例
2024-01-12 20:16:06
Mysql语法、特殊符号及正则表达式的使用详解
2024-01-12 21:56:50
必须知道的10个不常用HTML标签[译]
2009-03-31 13:19:00
Python中的文件和目录操作实现代码
2022-08-30 15:12:11
为网站代码块pre标签增加一个复制代码按钮代码
2024-04-10 10:49:27