简体   繁体   English

在openpyxl条件格式公式中使用多个单元格

[英]Use multiple cells in openpyxl conditional formatting formula

I am trying to write a spreadsheet in Python which uses conditional formatting to find the largest of three cells in a row and give it a green fill.我正在尝试在 Python 中编写一个电子表格,它使用条件格式来查找连续三个单元格中最大的一个并给它一个绿色填充。 I have been able to do it for the comparison of two cells, but there is very little information about how to write a formula in the openpyxl documentation that I can see, and there is similarly very little on StackOverflow about the problem.我已经能够比较两个单元格,但是在我可以看到的 openpyxl 文档中关于如何编写公式的信息很少,而且 StackOverflow 上关于这个问题的信息也很少。

For two cells, which worked as desired, my code was the below:对于两个按需要工作的单元格,我的代码如下:

sheet.conditional_formatting.add(f'G${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'H${row+iter_num}'], fill=greenfill))
sheet.conditional_formatting.add(f'H${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'G${row+iter_num}'], fill=greenfill))

The {row+iter_num} is required as this is used in a for loop. {row+iter_num}是必需的,因为它用于 for 循环。

To do this comparison for more cells, I tried changing the formula to include and :为了对更多单元格进行此比较,我尝试将公式更改为包含and

sheet.conditional_formatting.add(f'G${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'H${row+iter_num}' and f'I${row+iter_num}'], fill=greenfill))
sheet.conditional_formatting.add(f'H${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'G${row+iter_num}' and f'I${row+iter_num}'], fill=greenfill))
sheet.conditional_formatting.add(f'I${row+iter_num}', CellIsRule(operator='greaterThanOrEqual', \
formula=[f'G${row+iter_num}' and f'H${row+iter_num}'], fill=greenfill))

在此处输入图像描述

I am not sure if using and is logical in this situation, but like I said there is very little in the documentation.我不确定在这种情况下使用and是否合乎逻辑,但就像我说的那样,文档中的内容很少。 I also cannot use any comparisons to find which of G, H and I are largest in Python as they are determined by excel functions.我也无法使用任何比较来找出 Python 中的 G、H 和 I 中哪个最大,因为它们是由 excel 函数确定的。 For the image above, the green cells for each row should be 200, 16, 82, 1890, 150 (second), 1025, 527, 392, 150 (second), there should only be one green cell per row.对于上图,每行的绿色单元格应该是 200、16、82、1890、150(秒)、1025、527、392、150(秒),每行应该只有一个绿色单元格。

If doing this directly in Excel, I would use a formula.如果直接在 Excel 中执行此操作,我会使用公式。 You'll want to put that same formula into the formula parameter to Rule().您需要将相同的公式放入 Rule() 的公式参数中。 Here's a complete working example:这是一个完整的工作示例:

from openpyxl import Workbook
from openpyxl.formatting import Rule
from openpyxl.styles import Font, PatternFill, Border
from openpyxl.styles.differential import DifferentialStyle

wb = Workbook()
ws = wb.active

ws.append([1, 2, 3])
ws.append([3, 1, 2])
ws.append([2, 3, 1])
ws.append([1, 2, 3])
ws.append([3, 1, 2])
ws.append([2, 3, 1])

green_fill = PatternFill(bgColor="00FF00")
dxf = DifferentialStyle(fill=green_fill)
r = Rule(type="expression", dxf=dxf, formula=["=A1=MAX($A1:$C1)"])
ws.conditional_formatting.add('A1:C6', r)

# Save the file
wb.save("sample.xlsx")

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

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