Skip to content Skip to sidebar Skip to footer

Excel Columns Comparison In Generic Way And Highlight The Differences With Some Color

I am trying to convert a csv file to excel as per below code and i have few columns which need to be compared inside excel and do conditional format In the below code i have hard

Solution 1:

If you're asking how to make

worksheet.conditional_format('C2:C1048576', {'type':'formula',
                                   'criteria':'=B2<>C2',
                                   'format':orange_format})

reusable to other columns, then try something like this:

defget_previous_column(column):
    # if you're expecting to go above column `Z` then you'll need to improve this functionreturnchr(ord(column) - 1)

defmake_conditional_format(worksheet, col_src, col_tgt):
    column_range = f"{col_tgt}2:{col_tgt}1048576"
    worksheet.conditional_format(
        column_range, 
        {
            'type':'formula',
            'criteria':f'={col_src}2<>{col_tgt}2',
            'format':orange_format
         }
    )

columns_to_format = ['C', 'E', 'K', 'M']

for col_tgt in columns_to_format:
    col_src = get_previous_column(col_tgt)
    make_conditional_format(worksheet, col_src, col_tgt)

Post a Comment for "Excel Columns Comparison In Generic Way And Highlight The Differences With Some Color"