python - 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)
解决方案
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)
您可能需要使用颜色索引来为您的红色找到正确的索引。我刚刚抓起一个我知道是红色的单元格并检查了颜色索引。更多信息可以在这里找到
推荐阅读
- python - 如何在 python 的变量中存储 aws 组织单元 ID 和 scp 策略 ID?
- sql - 如何使用 `jsonb_build_object` 创建具有聚合函数和动态键值的嵌套 JSON 返回
- ios - ionic 3 - iOS 策略位置权限模式警报消息
- c++ - 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- react-native - ReactNative componentDidMount 没有被调用
- javascript - 具有 Firebase 云功能的排行榜
- matlab - 如何为两个耦合的二阶微分方程系统实现六个初始条件?
- node.js - Node.js & amcharts4 - 如何通过“Javascript”正确集成和调用 core.js 和 charts.js
- javascript - 为什么 setInterval 调用我的函数一次?
- c++ - 编译时的 std::experimental::source_location