Search code examples
excelpython-3.xopenpyxl

Getting Excel cell background themed color as hex with openpyxl


I'm reading *xlsx files with openpyxl python library, and within other data I want to get information on cells background color.

This code I was using normally, while my worksheets contained only standard colors, then it returned me something like "FFFFC000" in a string format that I can use further.

color = cell.fill.start_color.index

After I've met documents containing cells of themed colors it returns me just integers like '1' or '9' for such cells, as I can guess it equals to column of theme color from the menu:

patternfill

Is there any way to get hex or rgb info on such themed colors?


Solution

  • After hours of googling finally found the solution - proposed idea to add converting between theme + tint to RGB in openpyxl. Importing their set of color converting functions, I can reduce my solution to the next steps:

    wb = load_workbook(filename, data_only=True)
    theme = cell.fill.start_color.theme
    tint = cell.fill.start_color.tint
    color = theme_and_tint_to_rgb(wb, theme, tint)