使用 python 中的 xlrd 计算 excel 文件中带有红色文本的单元格数

Count the number of cells with red text in excel file using xlrd in python

我正在使用 xlrd 在我的计算机中打开一个 excel 文件,我有红色数字,黑色数字,我想计算红色数字的数量,有没有人知道如何计算接近这个?

import xlrd

filename = "data.xls"
book = xlrd.open_workbook(filenmae, formatting_info = True)
import xlrd

filename = 'data.xls'
book = xlrd.open_workbook(filename, formatting_info=True)
sheet = book.sheet_by_index(0)
max_row = sheet.nrows
max_col = sheet.ncols
count = 0
for row in range(max_row):
    for col in range(max_col):
        cell = sheet.cell(row, col)
        frmt = book.xf_list[cell.xf_index]
        font = book.font_list[frmt.font_index]
        count = count+1 if font.colour_index == 10 else count

print(count)

您可能需要使用颜色索引来找到适合您的红色的正确索引。我只是抓住了一个我知道是红色的细胞并检查了颜色索引。可以找到更多信息 here