Ignore blank cell to format in python xlsxwriter

Viewed 366

I am using xlsxwriter module to format a excel file. I have blank cell in one column and I don not want to format those empty cell. How can do it?

worksheet.conditional_format('F1:F12', {'type': 'cell', 'criteria': '<=', 'value': 0, 'format': bold})

this format is applicable for entire column but I don not want to include empty cell in this format.

1 Answers

To figure out a conditional format in XlsxWriter you should figure it out in Excel first and then transfer it across.

In this case you have more than one criteria so it is probably best to use a formula. Something like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('conditional_format.xlsx')
worksheet = workbook.add_worksheet()

# Add a format. Light red fill with dark red text.
format1 = workbook.add_format({'bg_color': '#FFC7CE',
                               'font_color': '#9C0006'})


# Write some sample data.
data = [1, 0, 2, -1, "", -1, "", 1]
worksheet.write_column(0, 0, data)


# Write a conditional format over a range.
worksheet.conditional_format(0, 0, len(data) -1, 0,
                             {'type': 'formula',
                              'criteria': '=AND(A1<=0,A1<>"")',
                              'value': 0,
                              'format': format1})

workbook.close()

Output:

enter image description here

Related