[英]How do I change the font color on every other row in an excel spreadsheet using openpyxl?
I need to change every other row's text color to a different color. 我需要将每个其他行的文本颜色更改为不同的颜色。 I know how to loop through each row, but I don't know how to change the font color of the current cell. 我知道如何遍历每一行,但我不知道如何更改当前单元格的字体颜色。 How do I do that? 我怎么做?
I see how to change the font color on each individual cell, but how do I loop through and do it to many cells? 我看到如何更改每个单元格的字体颜色,但是如何循环并对多个单元格进行操作?
def open_excel_file(file_name,worksheet_name):
wb = load_workbook(file_name)
ws = wb[worksheet_name]
row_count = ws.max_row
for x in range(1, row_count):
print(x, ws.cell(row=x, column=1).value)
open_excel_file('craigslist.xlsx', 'motorcycle')
With the help of Henry Yik and Charlie Clark above I came up with this as a solution. 在Henry Yik和Charlie Clark的帮助下,我提出了这个解决方案。 Thanks Guys 多谢你们
def open_excel_file(file_name,worksheet_name):
wb = load_workbook(file_name)
ws = wb[worksheet_name]
row_count = ws.max_row
for x in range(1, row_count):
c = ws.cell(row=x, column=1)
if x % 2 != 0:
c.font = Font(size=12, color=RED)
wb.save(file_name)
open_excel_file('craigslist.xlsx', 'motorcycle')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.