gpt4 book ai didi

python - 在 Python 的 openpyxl 中用颜色填充行 Excel

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:29 24 4
gpt4 key购买 nike

如果预先不知道要填充的行号,如何在 openpyxl 中用颜色填充行?它是位于未知数据行部分开头的标题行,例如:

from openpyxl import load_workbook, Workbook
from openpyxl.styles import PatternFill
import random
wb = Workbook()
ws = wb.active

for section in range(10):
header_row = ('col1', 'col2')
ws.append(header_row) # Wanted to fill this
random_amount_rows = [('bar', 'foo') for i in range(1, random.randrange(2,10))]
for datarow in random_amount_rows:
ws.append(datarow)

一般情况下,当行号已知时,可以填写如下:

fill = PatternFill("solid", fgColor="DDDDDD")
for cell in list(ws.rows)[rownumber]:
cell.fill = fill

最佳答案

通过只写模式工作如下。谢谢,查理·克拉克。

from openpyxl.worksheet.write_only import WriteOnlyCell
fill = PatternFill("solid", fgColor="DDDDDD")

for section in sections:
row_to_fill_texts = ('col1', 'col2')
row = []
for h in row_to_fill_texts:
cell = WriteOnlyCell(ws, value=h)
cell.fill = fill
row.append(cell)
ws.append(row)
# adding other lines

关于python - 在 Python 的 openpyxl 中用颜色填充行 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50341962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com