简体   繁体   English

使用条件识别行后的 Xlsxwriter 条件格式

[英]Xlsxwriter conditional formatting after identifying rows with criteria

Good afternoon,下午好,

Im trying to change the conditional formatting with xlsxwriter.我试图用 xlsxwriter 更改条件格式。

Im able to do so using我能够这样做使用

percent_format = workbook.add_format({'num_format': '0%', 'align': 'center', 'border': 1})

worksheet.conditional_format('B1:J%d' % (number_rows),
                                        {"type": "formula",
                                        "criteria": '=INDIRECT("B"&ROW())="THISROW"',
                                        "format": percent_format 
                                        })  

With this step I am able to assign any normal formatting, like bold, centered, etc, etc通过这一步,我可以分配任何正常的格式,如粗体、居中等

But I need to apply conditional formatting over those rows like 3 color scale etc但我需要对这些行应用条件格式,如 3 色标等

I guess my question is how to create a set of cell ranges and then apply 3 color cond formatting over them.我想我的问题是如何创建一组单元格范围,然后在它们上应用 3 颜色条件格式。

Thank you!谢谢!

EDIT 1:编辑1:

Adding more info:添加更多信息:

在此处输入图像描述

I want to apply 3 color cond formatting to the rows where THISROW is matched in the column我想对列中匹配 THISROW 的行应用 3 色条件格式

to C5:D5 C9:D9 C13:D13至 C5:D5 C9:D9 C13:D13

If you specifically want a 3 color scale it's如果你特别想要一个 3 色标,它是

worksheet7.conditional_format('I3:I14', {'type': '3_color_scale',
                                         'min_color': "#C5D9F1",
                                         'mid_color': "#8DB4E3",
                                         'max_color': "#538ED5"})

see examples here: https://xlsxwriter.readthedocs.io/example_conditional_format.html在此处查看示例: https://xlsxwriter.readthedocs.io/example_conditional_format.html

If you want a more complex output like your formula suggests then you are doing it properly but you need to repeat it numerous time (just like you would in Excel).如果您想要一个更复杂的 output 就像您的公式建议的那样,那么您做得正确,但您需要重复多次(就像在 Excel 中一样)。 If your formula doesn't work it's probably because the Excel formula doesn't work in Excel.如果您的公式不起作用,可能是因为 Excel 公式在 Excel 中不起作用。

This这个

'=INDIRECT("B"&ROW())="THISROWS"' '=INDIRECT("B"&ROW())="THISROWS"'

is two volatile functions where none is required.是两个不需要的易失性函数。 If you don't put $ in the adress of the cell the position is already relative.如果您不将 $ 放在单元格的地址中,则 position 已经是相对的。 Hence if you start your area in B3 and end it in F15, for instance, if you write因此,如果你在 B3 开始你的区域并在 F15 结束它,例如,如果你写

'=B3="THISROWS"' '=B3="THISROWS"'

then in B4, the B3 in your code will be replaced by B4.然后在 B4 中,您代码中的 B3 将被 B4 替换。 If you need it to move in the rows but not the columns write:如果您需要它在行中移动而不是在列中移动,请写入:

'=$B3="THISROWS"' '=$B3="THISROWS"'

This is not the way I intended to solve this, but using the xlxswriter package.. but here we go.这不是我打算解决这个问题的方式,而是使用 xlxswriter package .. 但这里我们是 go。

color_format = workbook.add_format({'bold': True, 'color': 'red'})
for i in range(len(df.index)/5):                                        
    worksheet.conditional_format('C{}:D{}'.format(5+4*i,5+4*i), {'type':     'cell',
                                        'criteria': '>=',
                                        'value':    1,
                                        'format':   color_format})

I'm ashamed to do it this way tho... now anything over 1 gets in red color...我很惭愧这样做......现在任何超过1的东西都会变成红色......

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

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