简体   繁体   English

Python - Openpyexcel - 条件格式,到底如何将规则应用于列,但突出显示该行?

[英]Python - Openpyexcel - Conditional formatting, How on earth do i apply the rule to a column, but highlight the row?

import openpyxl
from openpyxl.styles import PatternFill
from openpyxl.formatting.rule import CellIsRule

load the workbook
wb = openpyxl.load_workbook('output.xlsx')
get the active sheet
ws = wb.active
#make the value of L3 "P"
ws["L3"].value = "P"
>#make the value of L4 "F"
ws["L4"].value = "F"
#make the value of L5 "+"
ws["L5"].value = "+"
#make the value of L6 "-"
ws["L6"].value = "-"

blueFill = PatternFill(start_color='ADD8E6', end_color='ADD8E6', fill_type='solid')
rule1 = CellIsRule(operator='equal', formula=['$L$3'], stopIfTrue=True, fill=blueFill)

greenFill = PatternFill(start_color='90EE90', end_color='90EE90', fill_type='solid')
rule2 = CellIsRule(operator='equal', formula=['$L$4'], stopIfTrue=True, fill=greenFill)

redFill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')
rule3 = CellIsRule(operator='equal', formula=['$L$5'], stopIfTrue=True, fill=redFill)

yellowFill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')
rule4 = CellIsRule(operator='equal', formula=['$L$6'], stopIfTrue=True, fill=yellowFill)

#add the rules to column K
ws.conditional_formatting.add('K2:K1000', rule1)
ws.conditional_formatting.add('K2:K1000', rule2)
ws.conditional_formatting.add('K2:K1000', rule3)
ws.conditional_formatting.add('K2:K1000', rule4)

 #save the changes
wb.save('output2.xlsx')


So ive made it this far and everything works the way i want it to, except, i cant seem to find a way to Highlight the row the cell is found on and not just the cell itself.所以我已经做到了这一点,一切都按照我想要的方式工作,除了,我似乎无法找到一种方法来突出显示单元格所在的行,而不仅仅是单元格本身。

I just cant think of a way to write it.我只是想不出一种方法来写它。 Google has led me down some dead ends, so im hoping someone out there has accomplished this and can let me in on the trick!`谷歌让我陷入了死胡同,所以我希望有人已经完成了这个并且可以让我参与其中!`

If you have no luck with changing your code to extend the range, the following code sample will apply the CF as formulas across the col range A - K. If you type any of your values in column K from K2 down (to K1000) the row from A to K is highlighted in your selected colour.如果您无法通过更改代码来扩展范围,则以下代码示例将在 A - K 列范围内应用 CF 作为公式。如果您在 K 列中从 K2 向下(到 K1000)键入任何值,则从 A 到 K 的行以您选择的颜色突出显示。 See example image at the bottom.请参阅底部的示例图像。

import openpyxl
from openpyxl.styles import PatternFill
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting.rule import Rule


wb = openpyxl.load_workbook('output.xlsx')
# get the active sheet
# ws = wb.active
ws = wb['Sheet1']
# make the value of L3 "P"
ws["L3"].value = "P"
# make the value of L4 "F"
ws["L4"].value = "F"
# make the value of L5 "+"
ws["L5"].value = "+"
# make the value of L6 "-"
ws["L6"].value = "-"

### Colour fill formats
blueFill = PatternFill(start_color='ADD8E6', end_color='ADD8E6', fill_type='solid')
greenFill = PatternFill(start_color='90EE90', end_color='90EE90', fill_type='solid')
redFill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')
yellowFill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')

### Fill range with sheet row location
fill_dict = {3:blueFill, 4:greenFill, 5:redFill, 6:yellowFill}
### Conditional format range so a fill row will be across columns A to K
cf_range = '$A$2:$K$1000'
### Add Conditional Formats to the sheet
for row, fill in fill_dict.items():
    rule = Rule(type='expression', dxf=DifferentialStyle(fill=fill), stopIfTrue=True)
    rule.formula = [f'$L${row}=$K2']
    ws.conditional_formatting.add(cf_range, rule)

# save the changes
wb.save('output2.xlsx')

样本表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM