Jea*_* T. 5 python excel openpyxl
我想openpyxl使用 Excel 时通常使用的换位功能将公式从一个单元格复制粘贴到另一个单元格。
例如,将公式复制=SUM(J3:J18)到下一列会自动将其更改为=SUM(K3:K18).
我发现这种方法可以通过使用来工作win32com.client,但我不知道openpyxl.
有没有办法使用正则openpyxl表达式来执行此操作,或者我应该手动替换所有列号?
谢谢
openpyxl提供(至少现在)一些通过Translator类翻译公式的工具。在与 Formulas tokenizer相关的页面中提到:
这是有关如何使用它的示例。
>>> 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)'
Run Code Online (Sandbox Code Playgroud)
这是翻译公式的函数的当前原型:
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).
"""
Run Code Online (Sandbox Code Playgroud)