xlsxwriter 条件格式 [英] xlsxwriter conditional format

查看:82
本文介绍了xlsxwriter 条件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很直接,我可以在执行条件格式时传递多个类型值对,例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆