尝试使用 Python select excel 个具有红色和绿色背景的单元格

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

我有一个文件,其中绿色突出显示的单元格和红色突出显示的单元格指示严重级别。

现在我正在使用 StyleFrame 来提取它们,但出于某种原因我可以提取红色单元格但不能提取绿色单元格。知道如何解决这个问题吗?

请注意,我确保代码中指定的颜色与文件中的颜色相同。

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

结果:

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: []

感谢@OsmosisJonesLoL,我在通常的 6 位绿色十六进制代码之前添加了 "FF",它起作用了!

最终代码:

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