openpyxl库突出显示电子表格中的多个单元格

时间:2018-08-04 06:00:57

标签: python excel openpyxl

为了说明这一点,我正在运行一些测试,并编写了一个程序,该程序生成用于数据分析的电子表格。

对于每个测试,已经在随机位置(我编写了将这些数字作为字符串列表存储在程序中)的某些数字已经写入电子表格,我希望用颜色突出显示/标记这些数字。

我有以下几行用python用openpyxl库编写的代码:

if trackPgmBits:
    ws = wb['Pgm tracking']
    ws.append([''])
    for j in range(len(pgm_tracking_summary)):
        for i in range(len(pgm_tracking_summary[j])):
            if analog_tracking_summary[j][i] == '':
                pgm_tracking_summary[j][i] = ''
        ws.append(pgm_tracking_summary[j])



    RRule = CellIsRule(operator='equal', formula=highlightPgmBits, stopIfTrue = True, fill = PatternFill(start_color='FFFF00', end_color='FFFF00' ,fill_type='solid'))
    RRange = "A1:"+_get_column_letter(ws.max_column) + str(ws.max_row)
    ws.conditional_formatting.add(RRange, RRule)  

我要突出显示的数字数组存储在highlightPgmBits数组中,并通过以下公式传递到程序中:Formula = highlightPgmBits'

** highlightPgmBits是字符串列表。

当我将其替换为硬编码的单个值时,此代码有效:Formula = ['2.4087']

当我用两个硬编码的值替换时,此代码不起作用:Formula = ['2.4087','2.7']

这使我相信我的方法仅适合于突出显示一个数字。但是,一定有办法解决这个问题!有任何想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

工作解决方案:

我本质上需要遍历我创建的列表的每个元素,以便为要突出显示的每个数字制定新的“规则”:

尽管效率低下,但可以解决问题。我还根据要监视的特定条件添加了不同的颜色涂层。

if trackPgmBits:
    ws = wb['Pgm tracking']
    print 'highlightPgmBits'
    print highlightPgmBits
    ws.append([''])
    for j in range(len(pgm_tracking_summary)):
        for i in range(len(pgm_tracking_summary[j])):
            if analog_tracking_summary[j][i] == '':
                pgm_tracking_summary[j][i] = ''
        ws.append(pgm_tracking_summary[j])

    for j in range(len(highlightPgmBits)):
        if highlightPgmBits_Cycle < (cycles-1)/4:
            RRule = CellIsRule(operator='equal', formula=[highlightPgmBits[j]], stopIfTrue = True, fill = PatternFill(start_color='FFA600', end_color='FFA600' ,fill_type='solid'))
            RRange = "A1:"+_get_column_letter(ws.max_column) + str(ws.max_row)
            ws.conditional_formatting.add(RRange, RRule)
        elif (highlightPgmBits_Cycle[j] < (cycles-1)/2) and (highlightPgmBits_Cycle[j] >= (cycles-1)/4):
            RRule = CellIsRule(operator='equal', formula=[highlightPgmBits[j]], stopIfTrue = True, fill = PatternFill(start_color='FFFF00', end_color='FFFF00',fill_type='solid'))
            RRange = "A1:"+_get_column_letter(ws.max_column) + str(ws.max_row)
            ws.conditional_formatting.add(RRange, RRule)
        elif (highlightPgmBits_Cycle[j] < (cycles-1)*(3/4)) and (highlightPgmBits_Cycle[j] >= (cycles-1)/2):
            RRule = CellIsRule(operator='equal', formula=[highlightPgmBits[j]], stopIfTrue = True, fill = PatternFill(start_color='00F7FF', end_color='00F7FF' ,fill_type='solid'))
            RRange = "A1:"+_get_column_letter(ws.max_column) + str(ws.max_row)
            ws.conditional_formatting.add(RRange, RRule)
        else: 
            RRule = CellIsRule(operator='equal', formula=[highlightPgmBits[j]], stopIfTrue = True, fill = PatternFill(start_color='73FF00', end_color='73FF00' ,fill_type='solid'))
            RRange = "A1:"+_get_column_letter(ws.max_column) + str(ws.max_row)
            ws.conditional_formatting.add(RRange, RRule)

Looks like this

如果您要实现类似的功能,请发表评论,我的帖子似乎令人困惑,我可以澄清一下!另外,如果您知道更有效的解决方案,请分享!