详解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_typemin_value,mid_value,max_valuemin_color,mid_color,max_colorbar_color,bar_only,bar_solidbar_negative_color,bar_border_colorbar_negative_border_color,bar_negative_color_samebar_negative_border_color_same,bar_no_borderbar_direction,bar_axis_position,bar_axis_colordata_bar_2010,icon_style,iconsreverse_icons,icons_only,stop_if_truemulti_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"的单元格背景色为绿色。
热门推荐
净土法门的十种信念与八大要领
为什么下水道会堵?专业疏通下水道
换屏哥教你搞定iPhone屏幕乱跳
AI训练平台:手机屏幕检测的智能化革新
细胞分裂:生命科学的神奇魔法
青春期少女如何通过饮食改善贫血?
破冰之路:地中海贫血患者的职场突围
如何正确就医治疗缺铁性贫血?
贫血诊断新标准:你的CBC正常吗?
阳朔西街:中西合璧的千年古街
广州重要的历史遗址,就在北京路步行街附近,游客却不多?
三元里村:千年古村与抗英斗争的历史见证
阳朔最美瞬间:手机摄影技巧大揭秘
老年人该如何安全有效地应用阿司匹林?
专家推荐:霍乱疫苗和伤寒疫苗防腹泻
夏季腹泻后,这样吃最安全!
细菌性腹泻家庭护理全攻略:从症状识别到用药指南
秋冬腹泻用药指南:蒙脱石散vs左氧氟沙星,谁更胜一筹?
11年前宫斗戏,殷桃在前杨幂在后,为什么孙俪《甄嬛传》最得人心?
云和梯田,5A!
什么是绿色建筑,为何在可持续生活中扮演着不可或缺的角色?
竹材+低VOC涂料:打造绿色家居新潮流
阜宁装修网教你如何避免装修预算超支?
装修合同避坑指南:如何选到靠谱的装修公司?
2025装修新趋势:从环保到智能,打造理想家居的六大设计灵感
春节后健康饮食指南,营养师教你吃出好身材!
中医专家详解:春节后如何调理身体,恢复健康状态?
春节返工潮:基层劳工提前开工抢时间
春节假期综合症来袭?这份应对指南请收好!
春日打卡都江堰:青城山、离堆公园、南桥必玩!