xlsxwriter 条件格式 [英] xlsxwriter conditional format
问题描述
我的问题很直接,我可以在执行条件格式时传递多个类型值对,例如:
worksheet.conditional_format(第一行,第一列,最后一行,最后一列,{"type": ["no_blanks", "blanks"],格式":abc})
在执行此操作时,我收到以下错误:类型错误:不可散列的类型:'list'
在 Xlsxwriter 文档中关于 worksheet.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'
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:
这篇关于xlsxwriter 条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!