简体   繁体   English

如何使用openpyxl更改Excel电子表格中每隔一行的字体颜色?

[英]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.

相关问题 如何使用 openpyxl python 库或任何其他 python 模块从 excel 列值中获取字体颜色 - How to fetch font color from excel column values using openpyxl python library or any other python module 如何使用openpyxl遍历excel电子表格中的每一行 - how to loop through each row in excel spreadsheet using openpyxl 如何使用 OpenPyXL 列出 Excel 电子表格中的所有第一行值? - How can I list all 1st row values in an Excel spreadsheet using OpenPyXL? 在 Python 中使用 openpyxl 将行插入 Excel 电子表格 - Insert row into Excel spreadsheet using openpyxl in Python 如何使用openpyxl在特定的Excel单元格中填充颜色? - How do I color-fill in a specific Excel cell using openpyxl? 我想每天更新excel电子表格中的最后一行。 OpenPyXl - I want to update the last row in an excel spreadsheet, daily. OpenPyXl 如何使用openpyxl和python3为excel工作表中的一系列单元格(列和行)提供字体颜色? - How to give font color to a range of cells (columns and rows) in excel worksheet using openpyxl and python3? 如何使用元组和openpyxl对电子表格行中的最后一个元素求和? - how do you sum the last element in a spreadsheet row using tuples and openpyxl? Python中使用openpyxl,如何返回当前行号? - Using openpyxl in Python, how do I return the current row number? 如何在使用 openpyxl 时根据在 Excel 工作表中迭代的行号创建动态变量? - How do I create a dynamic variable based on the row number I am iterating over in an Excel sheet while using openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM