以下将根据实际使用的规则提取具有某些格式的CF详细信息.
默认情况下, colored颜色 是十六进制,但如果您想看到"黄色"而不是"FFFF 00",可以交叉匹配,但十六进制似乎没问题.
from openpyxl import load_workbook
wb_openpyxl = openpyxl.load_workbook(filename)
ws_openpyxl = wb_openpyxl["Sheet1"]
print('')
cs_list = ['Start', 'End']
for cf in ws_openpyxl.conditional_formatting:
print(f"Conditional Format Range: {cf.cells}")
print("Rules")
for rule in cf.cfRule:
if rule.colorScale:
print(f"Rule: {rule.priority}: Color Scale")
for idx, cs in enumerate(rule.colorScale.cfvo):
print(f'{cs_list[idx]}: _Type: {cs.type} _Value: {cs.val} _Color: {rule.colorScale.color[idx].rgb[2:]}')
else:
print(f"Rule: {rule.priority}: Formula")
print(f"Operation: {'Use a formula' if rule.operator is None else f'Format {rule.operator}'} "
f"{str(rule.formula)[1:-1]} "
f"Stop if True: {'Disabled' if rule.stopIfTrue is None else 'Enabled'}")
print(f"bfColor: {rule.dxf.fill.bgColor.rgb[2:]}")
print(f"endColor: {rule.dxf.fill.end_color.rgb[2:]}")
print(f"fgColor: {rule.dxf.fill.fgColor.rgb[2:]}")
print('-----------------------')