Python 中的 Excel 条件格式公式 - 待打开 [已关闭]

问题描述 投票:0回答:1

我的代码如下。我想给变量textval ='G'赋值,然后将其传递到公式中。

worksheet = srcfile[sheetName]
rule = formatting.Rule(type='expression')
color_fill = styles.PatternFill(start_color=colorcode, 
end_color=colorcode, fill_type='solid')
xy = coordinate_from_string(cellObj.coordinate) 
col = column_index_from_string(xy[0]) # returns 1
collet = get_column_letter(col)
cellref = 
rule.formula = [f'ISNUMBER(SEARCH("G", {cellref}))']   ##--------HERE ---
rule.dxf     = 
styles.differential.DifferentialStyle(fill=color_fill)
worksheet.conditional_formatting.add(collet + "2:" + collet + str(len(worksheet[collet])), 1)
python
1个回答
1
投票

可以这样修改

rule.formula = [f'ISNUMBER(SEARCH({textval}, {cellref}))']
© www.soinside.com 2019 - 2024. All rights reserved.