[Python] Inconsistent font color between openpyxl and the excel rendered version

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I need to read an excel file in python and get the font color of one particular cell. I have this column that sometimes contains a red value (font color) that is correctly rendered in excel. When i load the same file in openpyxl i have inconsistent values. For example:

    • i have the "code" column that is rendered in excel with a black font. input: code_cell.font.color output: <openpyxl.styles.colors.Color object> Parameters: rgb=None, indexed=None, auto=None, theme=1, tint=0.0, type='theme'
    • I have the same output when i request the font colour of a red cell (for example "pr_cell1"): input: pr_cell1.font.color output: <openpyxl.styles.colors.Color object> Parameters: rgb=None, indexed=None, auto=None, theme=1, tint=0.0, type='theme' THIS TWO SHOULD BE DIFFERENT!
    • I have different result when i ask the colour of a red cell, that is not themed: input: giacord_cell.font.color output: <openpyxl.styles.colors.Color object> Parameters: rgb='FFFF0000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'

    There is a way to obtain the rendered color of a themed cell? how is it possible that excel sucesfully render the color while openpyxl can tell the differences between two themed cell?

    Thank you.

    this is the code i used to read the excel file.

    from openpyxl import Workbook, load_workbook

    wb = load_workbook(file_path, rich_text=True) ##file_path is a string
    ws = wb.active
    rows = [ i for i in ws.iter_rows()]

    code_cell = r[code_index] ##integer index
    pr_cell= r[pr_index] ##integer index
    pr_cell1= r[pr_index1] ##integer index
    giacord_cell = r[giacord_index] ##integer index

    Continue reading...

Compartilhe esta Página