用python如何繪制表格不同顏色的excel
需求:

需求簡(jiǎn)單:但是感覺(jué)最后那部分遍歷有意思:S型數(shù)組賦值,考慮到下標(biāo),簡(jiǎn)單題
先實(shí)現(xiàn)個(gè)差不多的
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)
# 下面是學(xué)習(xí)讀取的部分代碼
# 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)

顏色沒(méi)對(duì)上,意思差不多就行了
總結(jié)
到此這篇關(guān)于用python如何繪制表格不同顏色excel的文章就介紹到這了,更多相關(guān)python繪制不同顏色excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django 實(shí)現(xiàn)購(gòu)物車功能的示例代碼
這篇文章主要介紹了Django 實(shí)現(xiàn)購(gòu)物車功能的示例代碼,實(shí)現(xiàn)了刪除產(chǎn)品和顯示購(gòu)物車的一系列購(gòu)物車的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python模擬登錄requests.Session應(yīng)用詳解
這篇文章主要介紹了Python模擬登錄requests.Session應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python如何實(shí)現(xiàn)質(zhì)數(shù)求和
這篇文章主要介紹了python如何實(shí)現(xiàn)質(zhì)數(shù)求和,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Python Numpy實(shí)現(xiàn)計(jì)算矩陣的均值和標(biāo)準(zhǔn)差詳解
NumPy(Numerical Python)是Python的一種開(kāi)源的數(shù)值計(jì)算擴(kuò)展。這種工具可用來(lái)存儲(chǔ)和處理大型矩陣,比Python自身的嵌套列表結(jié)構(gòu)要高效的多。本文主要介紹用NumPy實(shí)現(xiàn)計(jì)算矩陣的均值和標(biāo)準(zhǔn)差,感興趣的小伙伴可以了解一下2021-11-11
python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python類中super()和__init__()的區(qū)別
這篇文章主要介紹了python類中super()和__init__()的區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10

