abhilash mishra
abhilash mishra

Reputation: 255

xlsxwriter conditional format

My question is pretty straight, Can I pass multiple types values pair while doing conditional formatting like:

worksheet.conditional_format(first row, first col, last row, last col, 
                             {"type": ["no_blanks", "blanks"], 
                              "format": abc})

while doing this I'm getting the below error: TypeError: unhashable type: 'list'

Upvotes: 0

Views: 17261

Answers (1)

patrickjlong1
patrickjlong1

Reputation: 3823

In the Xlsxwriter documentation on the type parameter for the worksheet.conditional_format() (Link Here), you'll see in the documentation that there are no allowable parameters for type: 'blank' or type: 'no_blank'. So in order for you to make this work you'd have to do separate conditional formats.

Presumably you would like a different format for those cells that are blank versus those that are not blank. I've provided a reproducible example below that does this.

import pandas as pd
import xlsxwriter

first_row=1
last_row=6
first_col=0
last_col=1

df = pd.DataFrame({'Data': ['not blank', '', '', 'not blank', '', '']})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook  = writer.book
abc = workbook.add_format({'bg_color': 'red'})
efg = workbook.add_format({'bg_color': 'green'})
worksheet = writer.sheets['Sheet1']
worksheet.conditional_format(first_row, first_col, last_row, last_col,
                             {'type': 'blanks',
                             'format': abc})
worksheet.conditional_format(first_row, first_col, last_row, last_col,
                             {'type': 'no_blanks',
                             'format':    efg})
writer.save()

Expected Output:

Expected Output of test.xlsx

Upvotes: 9

Related Questions