详解xlsxwriter中conditional_format函数的高级应用
创作时间:
作者:
@小白创作中心
详解xlsxwriter中conditional_format函数的高级应用
引用
CSDN
1.
https://blog.csdn.net/liwenxiang629/article/details/138673004
在实际工作中,我们经常会遇到需要根据单元格的值或文本内容进行条件格式化的需求。本文将详细介绍如何使用xlsxwriter库中的conditional_format函数,实现这一功能。
在之前的文章中,我们已经介绍了xlsxwriter的基础API使用。在实际工作中,我们经常会遇到这样的需求:当单元格满足某个条件时,对其进行特定的格式化处理。这时候我们可以使用conditional_format
函数,它允许我们根据特定条件将格式应用于单元格或单元格区域,从而避免通过大量的条件语句来实现Excel中的条件过滤功能。
conditional_format函数说明
def conditional_format(self, first_row, first_col, last_row, last_col, options=None):
"""
Add a conditional format to a worksheet.
Args:
first_row: The first row of the cell range. (zero indexed).
first_col: The first column of the cell range.
last_row: The last row of the cell range. (zero indexed).
last_col: The last column of the cell range.
options: Conditional format options.
Returns:
0: Success.
-1: Row or column is out of worksheet bounds.
-2: Incorrect parameter or option.
"""
其中,first_row
、first_col
、last_row
、last_col
参数用于指定要格式化的单元格范围。我们重点需要关注options
参数,它包含多个可以自定义的选项:
type
: 指定条件格式的类型format
: 格式对象criteria
: 条件表达式value
: 值minimum
: 最小值maximum
: 最大值
其他参数还包括:
min_type
,mid_type
,max_type
min_value
,mid_value
,max_value
min_color
,mid_color
,max_color
bar_color
,bar_only
,bar_solid
bar_negative_color
,bar_border_color
bar_negative_border_color
,bar_negative_color_same
bar_negative_border_color_same
,bar_no_border
bar_direction
,bar_axis_position
,bar_axis_color
data_bar_2010
,icon_style
,icons
reverse_icons
,icons_only
,stop_if_true
multi_range
这里我们重点讲解type
参数。
Type: cell
当type
为cell
时,表示对单元格生效。常用的criteria
包括:
'criteria': '<'
'criteria': '<='
'criteria': '>'
'criteria': '>='
'criteria': 'between'
'criteria': 'not between'
Type: text
当type
为text
时,用于指定Excel的“特定文本”样式条件格式的匹配:
worksheet.conditional_format('A1:A4', {
'type': 'text',
'criteria': 'containing',
'value': 'foo',
'format': format1
})
常用的criteria
还包括:
'criteria': 'containing'
'criteria': 'not containing'
'criteria': 'begins with'
'criteria': 'ends with'
代码实例
我们通过下面的demo来进行演示:
实例 Type: cell
import xlsxwriter
workbook = xlsxwriter.Workbook('test2.xlsx')
worksheet = workbook.add_worksheet()
fill_yellow = workbook.add_format({'bg_color': 'FFAA33'})
fill_red = workbook.add_format({'bg_color': 'FF3333'})
fill_green = workbook.add_format({'bg_color': '00CC66'})
worksheet.conditional_format('A1:C1', {
"type": "cell",
"criteria": "<",
"value": 50,
"format": fill_red
})
worksheet.conditional_format('A1:C1', {
"type": "cell",
"criteria": "between",
"minimum": 50,
"maximum": 89,
"format": fill_yellow
})
worksheet.conditional_format('A1:C1', {
"type": "cell",
"criteria": ">=",
"value": 90,
"format": fill_green
})
worksheet.write_row(0, 0, [49, 60, 90])
workbook.close()
输出效果如下:
实例 Type: text
在上面代码的基础上增加:
worksheet.conditional_format('A2:C2', {
'type': 'text',
'criteria': 'containing',
'value': 'kevin',
'format': fill_green
})
worksheet.write_row(1, 0, ['kevin', 'mike', 'tony'])
执行代码后可以看到Excel里,包含"kevin"的单元格背景色为绿色。
热门推荐
李明德分享《蓬莱间》幕后花絮,展现演员专业素养
李明德星光大赏惊艳亮相,社交媒体热议:从争议到闪耀的逆袭之路
怀柔和密云,北京周边自驾游省钱秘籍
北京高速新规实施,老司机教你安全行车
李莹姐弟:坚韧精神与榜样力量的生动诠释
零排放、高效率:现代汽车新款氢燃料电池车亮相广州车展
氢气燃烧现淡蓝:能源转型的希望与挑战
车贷还清后必做三步:结清证明、解除抵押、变更车险
月入6000想买车?贷款购车前必看的四大要点
易鑫融资租赁遭汽车贷款诈骗,涉案金额超千万
全款还是贷款?年末购车前你需要知道这些
银行购车贷款:利率低至3%,审批规范,公务员有专属优惠
失恋疗愈DJ歌单:150首精选音乐,带你重拾微笑
女性失恋后的自我重塑之旅
露营热来袭,治愈失恋伤痛
失恋后靠健身逆袭,你也可以!
干眼与过敏性结膜炎:症状区分与联合治疗指南
心血管病的最后战场:心力衰竭
《小小的我》聚焦脑瘫患者生活,上映8天票房破5亿
突破古装美人标签,蒋勤勤凭《草木人间》获亚洲影后
揭秘公寓房物业费的隐藏猫腻
韩红再战《歌手》:华语乐坛的“救兵”来了!
韩红减重40斤:健康生活不是梦
国家统计局公布!去年房地产开发投资下降9.6%
咸菜真的不健康?真相揭秘!
苏武:从匈奴牧羊到麒麟阁功臣
黄酒储存全攻略:温度、密封、避光,这些细节决定酒质优劣
国标修订征求意见 葡萄酒到底要不要标注保质期?
婚姻情感维护指南:六大建议让爱常新
南宋名臣文天祥:抗元不屈,诗词传世