技术文档:
条件格式 — openpyxl 3.0.7 文档
openpyxl.formatting.rule module — openpyxl 3.1.3 documentation
但是,想让单元格数值按比例显示(右边正确)只能按data_bar_rule_2编写:
from openpyxl import Workbook
from openpyxl.styles import Color
from openpyxl.formatting.rule import DataBarRule
wb = Workbook()
sheet = wb.active
data = [5, 6, 7, 8, 9]
for i, value in enumerate(data, start=1):sheet[f'I{i}'] = valuesheet[f'J{i}'] = valuebar_color = Color(rgb="0000FF") # 蓝色
data_bar_rule_1 = DataBarRule(start_type='min', end_type='percentile', color=bar_color)
data_bar_rule_2 = DataBarRule(start_type='num', end_type='max', color=bar_color)
sheet.conditional_formatting.add('I1:I5', data_bar_rule_1)
sheet.conditional_formatting.add('$J$1:$J$5', data_bar_rule_2)
wb.save('data_bar_example.xlsx')
如果还是不理想,就自己组合搭配吧
内置格式包含一系列格式设置,这些设置类型与整数结合起来进行比较。可能的类型有:“num”、“percent”、“max”、“min”、“formula”、“percentile”。