用python如何繪制表格不同顏色的excel
需求:
需求簡(jiǎn)單:但是感覺最后那部分遍歷有意思: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)
顏色沒對(duì)上,意思差不多就行了
總結(jié)
到此這篇關(guān)于用python如何繪制表格不同顏色excel的文章就介紹到這了,更多相關(guān)python繪制不同顏色excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Selenium如何使用input標(biāo)簽上傳文件完整流程
這篇文章主要介紹了詳解Selenium如何使用input標(biāo)簽上傳文件完整流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表,無序鏈表詳解
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03Python一行代碼識(shí)別發(fā)票并保存Excel示例詳解
這篇文章主要為大家介紹了Python一行代碼識(shí)別發(fā)票并保存Excel示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python中處理無效數(shù)據(jù)的詳細(xì)教程
無效數(shù)據(jù)是指不符合數(shù)據(jù)收集目的或數(shù)據(jù)收集標(biāo)準(zhǔn)的數(shù)據(jù),這些數(shù)據(jù)可能來自于不準(zhǔn)確的測(cè)量、缺失值、錯(cuò)誤標(biāo)注、虛假的數(shù)據(jù)源或其他問題,本文就將帶大家學(xué)習(xí)Python中如何處理無效數(shù)據(jù),感興趣的同學(xué)可以跟著小編一起來學(xué)習(xí)2023-06-06python開發(fā)的自動(dòng)化運(yùn)維工具ansible詳解
ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開發(fā),集合了眾多運(yùn)維工具(puppet、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能,這篇文章主要介紹了python開發(fā)的自動(dòng)化運(yùn)維工具ansible詳解,需要的朋友可以參考下2021-08-08Python Pygame實(shí)現(xiàn)落球游戲詳解
本文主要介紹了利用Pygame實(shí)現(xiàn)落球小游戲,即屏幕上落下一個(gè)球,通過鼠標(biāo)移動(dòng),地下的木塊如果接上則加分,否則就減去一命,三條命用完則游戲結(jié)束。感興趣的可以學(xué)習(xí)2022-01-01