xlsxwriter 条件格式
xlsxwriter conditional format
我的问题很直截了当,我可以在进行条件格式设置时传递多个类型值对吗:
worksheet.conditional_format(first row, first col, last row, last col,
{"type": ["no_blanks", "blanks"],
"format": abc})
执行此操作时出现以下错误:
类型错误:不可散列的类型:'list'
在有关 worksheet.conditional_format()
(Link Here) 类型参数的 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()
预期输出:
我的问题很直截了当,我可以在进行条件格式设置时传递多个类型值对吗:
worksheet.conditional_format(first row, first col, last row, last col,
{"type": ["no_blanks", "blanks"],
"format": abc})
执行此操作时出现以下错误: 类型错误:不可散列的类型:'list'
在有关 worksheet.conditional_format()
(Link Here) 类型参数的 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()
预期输出: