python excel条件格式_条件格式 — openpyxl 3.0.5 文档

>>>from openpyxl import Workbook

>>>from openpyxl.styles import Color, PatternFill, Font, Border

>>>from openpyxl.styles.differential import DifferentialStyle

>>>from openpyxl.formatting.rule import ColorScaleRule, CellIsRule, FormulaRule

>>>

>>>wb = Workbook()

>>>ws = wb.active

>>>

>>># Create fill

>>>redFill = PatternFill(start_color='EE1111',

... end_color='EE1111',

... fill_type='solid')

>>>

>>># Add a two-color scale

>>># Takes colors in excel 'RRGGBB' style.

>>>ws.conditional_formatting.add('A1:A10',

... ColorScaleRule(start_type='min', start_color='AA0000',

... end_type='max', end_color='00AA00')

... )

>>>

>>># Add a three-color scale

>>>ws.conditional_formatting.add('B1:B10',

... ColorScaleRule(start_type='percentile', start_value=10, start_color='AA0000',

... mid_type='percentile', mid_value=50, mid_color='0000AA',

... end_type='percentile', end_value=90, end_color='00AA00')

... )

>>>

>>># Add a conditional formatting based on a cell comparison

>>># addCellIs(range_string, operator, formula, stopIfTrue, wb, font, border, fill)

>>># Format if cell is less than 'formula'

>>>ws.conditional_formatting.add('C2:C10',

... CellIsRule(operator='lessThan', formula=['C$1'], stopIfTrue=True, fill=redFill))

>>>

>>># Format if cell is between 'formula'

>>>ws.conditional_formatting.add('D2:D10',

... CellIsRule(operator='between', formula=['1','5'], stopIfTrue=True, fill=redFill))

>>>

>>># Format using a formula

>>>ws.conditional_formatting.add('E1:E10',

... FormulaRule(formula=['ISBLANK(E1)'], stopIfTrue=True, fill=redFill))

>>>

>>># Aside from the 2-color and 3-color scales, format rules take fonts, borders and fills for styling:

>>>myFont = Font()

>>>myBorder = Border()

>>>ws.conditional_formatting.add('E1:E10',

... FormulaRule(formula=['E1=0'], font=myFont, border=myBorder, fill=redFill))

>>>

>>># Highlight cells that contain particular text by using a special formula

>>>red_text = Font(color="9C0006")

>>>red_fill = PatternFill(bgColor="FFC7CE")

>>>dxf = DifferentialStyle(font=red_text, fill=red_fill)

>>>rule = Rule(type="containsText", operator="containsText", text="highlight", dxf=dxf)

>>>rule.formula = ['NOT(ISERROR(SEARCH("highlight",A1)))']

>>>ws.conditional_formatting.add('A1:F40', rule)

>>>wb.save("test.xlsx")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值