![](/img/trans.png)
[英]Copy column of cell values from one workbook to another with 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.