简体   繁体   中英

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. 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM