用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


猜你喜欢
- javascript/js的ajax的GET请求:<script type="text/javascript"&g
- request请求头信息的键会加上HTTP_转换成大写存到request.META中因此你只需要content_range = reques
- 如下所示:函数功能abs(x)返回一个数的绝对值。 参数可以是一个整数或浮点数。 如果参数是一个复数,则返回它的模。all(iterable
- 大部分情况下,这种动态生成的sql查询语句写法如下: 代码如下:select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 f
- CREATE TABLE [dbo].[TbGuidTable]( [TableName] [varchar](50) NOT NULL,
- 网络技术,无论是php还是java ,无论是pc时代,还是移动时代,无论是黑客技术,还是程序新人,it技术,无疑是当前最火的行业,无论是个人
- Flask-SQLAlchemy安装和建表操作请参考这里。 # Role表class Role(db.Model):
- 前面一篇我们已经把unittest的常用用法都已经讲过了,可能很多小伙伴有个疑问,unittest框架怎么做数据驱动呢?这节我们就来学习一下
- 废话不多说,直接上代码!updateBody = { "query":{ &nb
- 运行MySQL Server 5.0安装程序“setup.exe”,出现如下界面: 安装向导启动,按“Next”继续:
- 用python实现的抓取腾讯视频所有电影的爬虫# -*- coding: utf-8 -*-import reimport urllib2f
- 1.在浏览器搜索python.org,如下图选择第一个2.进入python官网,选择dowload然后选择windows如下图:3.选择py
- main.htm: <html>
- 1. 为什么要使用正则表达式?首先,大家来看一个例子。一个文本文件里面存储了一些市场职位信息,格式如下所示:Python3 高级开发工程师
- current_date()只显示的是当前时间的日期例如:select current_date() from a ;结果:2021-08-
- 本文主要介绍了pandas针对excel处理的实现,分享给大家,具体如下:读取文件import padasdf = pd.read_csv(
- 使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问
- 1.Python的基本数据类型数据类型想必大家都知道是什么含义,指的是输入数据的类型,任何数据都有明确的数据类型,例如我们输入100,这个数
- 1:php地址 http://127.0.0.6/?c=json2:java 输出的结果是[{"i
- 本文实例讲述了Python微信推送模板消息功能。分享给大家供大家参考,具体如下:官方文档:https://mp.weixin.qq.com/