abh*_*hra 0 python python-2.7 xlsxwriter
我的问题很直接,我可以在执行条件格式时传递多个类型值对,例如:
worksheet.conditional_format(first row, first col, last row, last col,
{"type": ["no_blanks", "blanks"],
"format": abc})
Run Code Online (Sandbox Code Playgroud)
这样做时,我收到以下错误: TypeError: unhashable type: 'list'
在 Xlsxwriter 文档中关于worksheet.conditional_format()(此处链接)的类型参数,您将在文档中看到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()
Run Code Online (Sandbox Code Playgroud)
预期输出:
| 归档时间: |
|
| 查看次数: |
6290 次 |
| 最近记录: |