以下 Python 代码使用 openpyxl 库遍历 Excel 工作表中的每一列,判断是否存在数值大于 1.6 或小于 0.3,并返回满足条件的列号。

import openpyxl

# 打开 Excel 文件
wb = openpyxl.load_workbook('example.xlsx')
# 获取第一个工作表
sheet = wb.active

# 从第一列开始遍历每一列
for col in sheet.columns:
    has_match = False
    # 遍历这一列的每一个单元格
    for cell in col:
        if isinstance(cell.value, (int, float)):
            if cell.value > 1.6 or cell.value < 0.3:
                has_match = True
                break
    # 如果存在符合条件的单元格,输出列号
    if has_match:
        print(col[0].column)

该程序打开名为 'example.xlsx' 的 Excel 文件,并遍历第一个工作表中的每一列。对于每一列,它遍历该列的每一个单元格,并检查单元格的值是否大于 1.6 或小于 0.3。如果找到符合条件的单元格,它会打印该列的列号。请注意,'col[0]' 表示该列中的第一个单元格,因此 'col[0].column' 是该列的列号。

Python 使用 openpyxl 库判断 Excel 列中是否存在特定数值