问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

详解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_rowfirst_collast_rowlast_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

typecell时,表示对单元格生效。常用的criteria包括:

  • 'criteria': '<'
  • 'criteria': '<='
  • 'criteria': '>'
  • 'criteria': '>='
  • 'criteria': 'between'
  • 'criteria': 'not between'

Type: text

typetext时,用于指定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"的单元格背景色为绿色。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号