pandas可不可以根据Excel单元格背景颜色筛选,具体如何操作呢?谢谢
解决方法
1、使用StyleFrame(包装 pandas),您可以将 excel 文件读入数据框,而不会丢失样式数据。
考虑以下工作表:
在此处输入图像描述
以及以下代码:
from styleframe import StyleFrame, utils
# from StyleFrame import StyleFrame, utils (if using version < 3.X)
sf = StyleFrame.read_excel('test.xlsx', read_style=True)
print(sf)
# b p y
# 0 nan 3 1000.0
# 1 3.0 4 2.0
# 2 4.0 5 42902.72396767148
sf = sf[[col for col in sf.columns
if col.style.fill.fgColor.rgb in ('FFFFFFFF', utils.colors.white)]]
# "white" can be represented as 'FFFFFFFF' or
# '00FFFFFF' (which is what utils.colors.white is set to)
print(sf)
# b
# 0 nan
# 1 3.0
# 2 4.0
或者
这个需求估计直接用openpyxl
可能更有可行性
比如:
import openpyxl
#打开名为fileName的工作簿
workbook = openpyxl.load_workbook(fileName)
#选择名为sheetName的表单
worksheet = workbook[sheetName]
#获取当前表单中单元格的行列数
rows, cols = worksheet.max_row, worksheet.max_column
test = []
#遍历。仅示例行
for i in range(rows):
#这里要注意一下,行列都是从1开始计数的
ce = worksheet.cell(row=i+1, column=6)
fill = ce.fill
#可以试着print一下每个单元格的颜色,对照excel文件,选取需要的颜色
# print(fill.start_color.rgb)
#我这里选择的是绿色和黄色
if fill.start_color.rgb == "FF92D050" or fill.start_color.rgb == "FFFFFF00":
print(ce.value)
test.append(ce.value)
如有问题及时沟通
https://www.pythonheidong.com/blog/article/958538/f8a9e31f4f60cb6cdd33/
这个问题可以看作如何获取背景色,用numpy+xlrd就行,参考上面的链接。然后根据颜色就筛选就行了
不行。建议从规范excel上考虑。