xlsxwriter conditional format

£可爱£侵袭症+ 提交于 2019-12-08 14:14:22

问题


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'


回答1:


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:



来源:https://stackoverflow.com/questions/49334585/xlsxwriter-conditional-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!