在openpyxl中使用Translator复制公式

时间:2018-12-10 20:43:39

标签: python excel formula openpyxl

我需要将公式从程序确定的列复制到相邻列,并且需要将公式单元格引用随列“移动”。例如,单元格E2中的公式为='Copy ATDs'!B60,我需要将该公式复制为单元格F2为='Copy ATDs'!C60 文档中的示例如下(原文如此):

>>> from openpyxl.formula.translate import Translator
>>> ws['F2'] = "=SUM(B2:E2)"
>>> # move the formula one colum to the right
>>> formula = Translator("=SUM(B2:E2)", "G2")
'=SUM(C2:F2)'

我理解这意味着,如果将第二个参数移到一列,则Translator函数将返回由一列转置的公式。

我正在使用的代码如下:

new_cell_ref = ws_dt.cell(column=source_col, row=r).offset(row=0, column=1).address()
ws_dt.cell(column=new_col, row=r).value = \
    Translator(ws_dt.cell(column=source_col, row=r).value, \ 
               new_cell_ref.column + str(new_cell_ref.row)).tokenizer.formula

Translator函数似乎并不关心第二个参数是什么。它始终返回原始公式,而无需翻译任何内容。请帮助我了解我在做什么错。

0 个答案:

没有答案