Python 如何使用xlsxwriter模块在多个单元格的excel中动态编写公式?

Python 如何使用xlsxwriter模块在多个单元格的excel中动态编写公式?,python,Python,我创建了excel工作簿,其中包含一些工作表,我添加了一个工作表,我想在该工作表中使用一个公式,从另一个工作表中获取值,为此,我必须使用for循环动态使用公式,但当我将cell2和cell3作为公式内的变量传递时,我得到了一个错误: File "<ipython-input-26-a7cfb4c63c79>", line 8, in <module> ws2.write_formula(0, 1, '=COUNTIFS(Deviation!%s:%s,"<=-15%

我创建了excel工作簿,其中包含一些工作表,我添加了一个工作表,我想在该工作表中使用一个公式,从另一个工作表中获取值,为此,我必须使用for循环动态使用公式,但当我将cell2和cell3作为公式内的变量传递时,我得到了一个错误:

File "<ipython-input-26-a7cfb4c63c79>", line 8, in <module>
ws2.write_formula(0, 1, '=COUNTIFS(Deviation!%s:%s,"<=-15%")' %(cell2, cell3))

TypeError: not enough arguments for format string`
文件“”,第8行,在

ws2.编写公式(0,1,'=COUNTIFS(偏差!%s:%s,“简化您的问题,省略excel内容,并单独使用打印字符串格式:

瞧,固定输出:

=COUNTIFS(Deviation!A1:B22,"<=-15%")
cell2 = "A1"
cell3 = "B22"

# TypeError: not enough arguments for format string
print('=COUNTIFS(Deviation!%s:%s,"<=-15%")' %(cell2, cell3))
cell2 = "A1"
cell3 = "B22"

formatstr = '=COUNTIFS(Deviation!%s:%s,"<=-15%%")' %(cell2, cell3)
print(formatstr)

# ws2.write_formula(0, 1, formatstr) # use formatted string
=COUNTIFS(Deviation!A1:B22,"<=-15%")
print('=COUNTIFS(Deviation!{}:{},"<=-15%%")'.format(cell2, cell3) )
print(f'=COUNTIFS(Deviation!{cell2}:{cell3},"<=-15%%")') # python 3