pythonexcelpandasxlrdstyleframe

Trying to select excel cells with red and green background using Python


I have a file that has green highlighted cells and red highlighted cells indicating severity level.

Right now I'm using StyleFrame to extract them but for some reason I can extract the red cells but not the green cells. Any idea of how to go about this?

Notice that I made sure that the colors specified in the code are the same as the file.

from StyleFrame import StyleFrame, utils


GeneralCategories = StyleFrame.read_excel('Categories.xlsx', read_style=True, use_openpyxl_styles=False)

def redBackground(cell):
    return cell if cell.style.bg_color in {utils.colors.red, 'FFFF0000'} else np.nan


def greenBackground(cell):
    return cell if cell.style.bg_color in {utils.colors.green, '00FF00'} else np.nan


SevereCategory = StyleFrame(GeneralCategories.applymap(redBackground).dropna(axis=(0, 1), how='all'))


NonSevereCategory = StyleFrame(GeneralCategories.applymap(greenBackground).dropna(axis=(0, 1), how='all'))  

Results:

print(SevereCategory)

                                    Keyword Categories
1               Adult Content: Nudity & Partial Nudity
2    Adult Content: Nudity & Partial Nudity;Adult C...
3    Adult Content: Nudity & Partial Nudity;Adult C...
4    Adult Content: Nudity & Partial Nudity;Adult C...
5    Adult Content: Nudity & Partial Nudity;Adult C...
..                                                 ...



print(NonSevereCategory)

Empty DataFrame
Columns: []
Index: []

Solution

  • Thanks to @OsmosisJonesLoL I added "FF" before the usual 6-digit green hex code and it worked!

    Final code:

    GeneralCategories = StyleFrame.read_excel('Keyword Category Breakout 12.5.19.xlsx', read_style=True, use_openpyxl_styles=False)
    
    def redBackground(cell):
        return cell if cell.style.bg_color in {utils.colors.red, 'FFFF0000'} else np.nan
    
    def greenBackground(cell):
        return cell if cell.style.bg_color in {utils.colors.green, 'FF00FF00', 'green'} else np.nan
    
    
    SevereCategory = StyleFrame(GeneralCategories.applymap(redBackground).dropna(axis=(0, 1), how='all'))
    
    NonSevereCategory = StyleFrame(GeneralCategories.applymap(greenBackground).dropna(axis=(0, 1), how='all'))