首页 > 解决方案 > Count the number of cells with red text in excel file using xlrd in python

问题描述

I am using xlrd to open an excel file in my computer and I have numbers in red, numbers in black, I want to count the number of numbers in red, do anyone have any idea how to approach this?

import xlrd

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

标签: pythonxlrd

解决方案


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)

您可能需要使用颜色索引来为您的红色找到正确的索引。我刚刚抓起一个我知道是红色的单元格并检查了颜色索引。更多信息可以在这里找到


推荐阅读