excel 根据单元格内的字体来筛选
import openpyxl
filename = “colortest.xlsx” # 读取 excel
workbook = openpyxl.load_workbook(filename)
worksheet = workbook.get_sheet_by_name(“Sheet1”) # 读取 Sheet
rows, cols = worksheet.max_row, worksheet.max_column
reddata = []
yellowdata = []
for i in range(1, rows):
for j in range(1, cols):
ce = worksheet.cell(row=i, column=j)
fill = ce.fill
font = ce.font
# print(fill.start_color.rgb)
if fill.start_color.rgb == "FFEEFA50":
yellowdata.append(ce.value)
if fill.start_color.rgb == "FFFF1D1D":
reddata.append(ce.value)
# if font.color.rgb == "FFFF1D1D":
# reddata.append(ce.value)
print(yellowdata, reddata)
这个地方需要注意一下。先判断有没有 font.color, 如果有的话,在去取 font.color.rgb. 否则,空指针,你懂得。