pythonexcelconditional-formattingxlsxwriter

How to add a min/max value in conditional formatting using xlsxwriter for python?


I am trying to create a shmoo plot with conditional formatting where the formatting rule knows the minimum value and does not mark the minimum value in the table with the minimum color, instead it knows the minimum value. For example, in a 10x10 table of points scored, the rule should not mark a cell with the lowest points, 80, with red color (say) but since it knows that the minimum possible value is a 0, it will place it on the color scale accordingly. example of a shmoo plot and the expected formula using the application

I am using xlsxwriter library in python 3.12

        worksheet.conditional_format(   #condtional formatting 3 color scale, colors in hex
        1,1,len(master_x_axis[i]),len(master_y_axis[i]),
        {
            "type": "3_color_scale",
            "min_color": "#ff6666",
            "mid_color": "#ffff66",
            "max_color": "#8cff66",
            "min_value": 0,
            "max_value": xlsxwriter.utility.xl_rowcol_to_cell(1,len(master_x_axis[i])+2)
        },
    )

I have tried the above piece of code and everything works except the "min_value" and "max_value" I cant see it get reflected on the excel file.


Solution

  • The example should set the min_type and max_type conditional format parameters to the required types, which in this case are probably "number" and "formula". The formula range should also be absolute.

    Something like this:

    import pandas as pd
    from xlsxwriter.utility import xl_rowcol_to_cell
    
    
    # Create a Pandas dataframe from some data.
    df = pd.DataFrame(
        {
            "A": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "B": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "C": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "D": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "E": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "F": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "G": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "H": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "I": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
            "J": [10, 10, 10, 10, 10, 10, 10, 10, 10, 0],
        }
    )
    
    # Create a Pandas Excel writer using XlsxWriter as the engine.
    writer = pd.ExcelWriter("pandas_conditional.xlsx", engine="xlsxwriter")
    
    # Convert the dataframe to an XlsxWriter Excel object.
    df.to_excel(writer, sheet_name="Sheet1")
    
    # Get the xlsxwriter workbook and worksheet objects.
    workbook = writer.book
    worksheet = writer.sheets["Sheet1"]
    
    # Get the dimensions of the dataframe.
    (max_row, max_col) = df.shape
    
    # Write the target max value:
    worksheet.write(1, max_col + 2, 10)
    
    
    # Apply a conditional format to the required cell range.
    worksheet.conditional_format(1, 1, max_row, max_col,
        {
            "type": "3_color_scale",
            "min_color": "#ff6666",
            "mid_color": "#ffff66",
            "max_color": "#8cff66",
    
            "min_type": "num",
            "min_value": 0,
    
            "max_type": "formula",
            "max_value": xl_rowcol_to_cell(1, max_col + 2, row_abs=True, col_abs=True),
        },
    )
    
    # Close the Pandas Excel writer and output the Excel file.
    writer.close()
    

    This give the following conditional format:

    enter image description here