简体   繁体   English

使用行号和列号而不是单元格地址Python编写Excel公式

[英]Writing Excel Formulas with Row and Column Numbers instead of Cell Address Python

I currently have the following code: 我目前有以下代码:

revenue_stats_feb = pd.DataFrame({'February' :['=D7/D40','=D7/D39','=D7/D37','=D7/D11','=D8/D7', '=D10/D8'],
                                  'merge': [1,2,3,4,5,6]})
    revenue_stats_feb = revenue_stats_feb[['February', 'merge']]

I was wondering if it is possible to write the formulas using the row and column numbers instead of the cell address, ie '=D7/D40' would be '=(3,6)/(3,39)' but when it gets exported to excel it still calculates as D7/D40 would. 我想知道是否可以使用行号和列号而不是单元格地址来编写公式,即'= D7 / D40'将是'=(3,6)/(3,39)'但是当它得到时导出到excel它仍然计算为D7 / D40会。

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

One way would be to use the functions XlsxWriter provides to convert (row, col) values to strings: 一种方法是使用XlsxWriter提供的函数将(row, col)值转换为字符串:

from xlsxwriter.utility import xl_rowcol_to_cell

formula = '=%s/%s' % (xl_rowcol_to_cell(6,  3),
                      xl_rowcol_to_cell(39, 3))

print(formula)  # =D7/D40

See the Working with Cell Notation section of the XlsxWriter docs for more details. 有关更多详细信息,请参阅XlsxWriter文档的“使用单元格表示法”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM