我的问题很直接,我可以在执行条件格式时传递多个类型的值对,例如:
worksheet.conditional_format(first row, first col, last row, last col,
{"type": ["no_blanks", "blanks"],
"format": abc})执行此操作时,我得到以下错误: TypeError: unhashable type:'list‘
发布于 2018-03-18 01:18:19
在关于worksheet.conditional_format() (链接在这里)的类型参数的Xlsxwriter文档中,您将在文档中看到type: 'blank'或type: 'no_blank'没有允许的参数。因此,为了使这一工作,您必须做单独的条件格式。
想必,对于那些空白的单元格和那些不是空白的单元格,您可能想要一种不同的格式。我在下面给出了一个可重复的例子。
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()预期输出:

https://stackoverflow.com/questions/49334585
复制相似问题