简体   繁体   中英

Xlsxwriter conditional formatting after identifying rows with criteria

Good afternoon,

Im trying to change the conditional formatting with 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

I guess my question is how to create a set of cell ranges and then apply 3 color cond formatting over them.

Thank you!

EDIT 1:

Adding more info:

在此处输入图像描述

I want to apply 3 color cond formatting to the rows where THISROW is matched in the column

to C5:D5 C9:D9 C13:D13

If you specifically want a 3 color scale it's

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

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). If your formula doesn't work it's probably because the Excel formula doesn't work in Excel.

This

'=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. Hence if you start your area in B3 and end it in F15, for instance, if you write

'=B3="THISROWS"'

then in B4, the B3 in your code will be replaced by B4. If you need it to move in the rows but not the columns write:

'=$B3="THISROWS"'

This is not the way I intended to solve this, but using the xlxswriter package.. but here we 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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