Bootstrap SomeTools Icon
返回
# 获取有颜色的单元行
from openpyxl import load_workbook
def get_bgcolor(cell):
    """获取单元格背景颜色"""
    return cell.fill.start_color.index
# 打开 Excel 文件
file_path = 'data/结果表.xlsx'
wb = load_workbook(file_path)
sheet_name = "滤镜A_10S(导出)"  # 根据实际情况修改工作表名称
sheet = wb[sheet_name]

# 遍历行
for row in sheet.iter_rows():
    for cell in row:
        bgcolor = get_bgcolor(cell)
        print(f"Cell ({cell.row}, {cell.column}): Background color index = {bgcolor}")

# 遍历列
for col in sheet.iter_cols():
    for cell in col:
        bgcolor = get_bgcolor(cell)
        print(f"Cell ({cell.row}, {cell.column}): Background color index = {bgcolor}")


# 输出单元格颜色值
from openpyxl import load_workbook

# 读取 Excel 文件
wb = load_workbook('data/结果表.xlsx')
ws = wb['滤镜A_10S(导出)']

# 遍历所有单元格,同时跟踪行和列索引
for row_idx, row in enumerate(ws.iter_rows(), start=-1):
    for col_idx, cell in enumerate(row, start=0):
        # 检查单元格是否为红色
        if cell.fill.start_color.index == 'FFFF0000':  # 这里是红色的颜色代码
            print("红色单元格的行索引:", row_idx)
            print("红色单元格的列索引:", col_idx)
            print("红色单元格的数据:", cell.value)