繁体   English   中英

使用openpyxl将公式从一个单元格复制到另一个单元格

[英]Copy formula from one cell to another using openpyxl

我想使用openpyxl将公式从一个单元格复制粘贴到另一个单元格,并使用Excel时通常会得到的转置。

例如,将公式=SUM(J3:J18)复制到下一列将自动将其更改为=SUM(K3:K18)

我发现此方法可以通过使用win32com.client但我不知道openpyxl等效openpyxl

有没有办法使用openpyxl做到这openpyxl还是我应该使用正则表达式手动替换所有列号?

谢谢

您将不得不手动执行此操作,但是您可能希望使用令牌处理程序,而不是编写自己的解析器。

openpyxl提供(至少现在)通过Translator类转换公式的工具。 在与公式标记程序有关的页面中已提及

这是有关如何使用它的示例。

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

这是用于转换公式的函数的当前原型:

def translate_formula(self, dest=None, row=None, col=None):
    """
    Convert the formula into A1 notation, or as row and column coordinates

    The formula is converted into A1 assuming it is assigned to the cell
    whose address is `dest` (no worksheet name).

    """

暂无
暂无

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

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