Python openpyxl: 当单元格的值为函数或方程时,如何计算单元格?

Python openpyxl: How can I compute the cells when the value of them are functions or equations?

我正在尝试使用库 openpyxl.

从一些 excel 文件中计算数据

现在我遇到了函数或方程的 cell.value 问题。

例如:

下面我可能有这样两个cell.value:

ws = openpyxl.load_work(xl_path)['Sheet1']
print(ws['B1'].value)
print(ws['B2'].value)
[output:]
= A1+A2    # Actually, the true value may be 1(A1) + 2(A2) -> 3
= A2*A4    # Actually, the true value may be 1(A1) * 4(A4) -> 4

当我计算它时,我得到的不是我想要的:

ws['C1'].value = ws['B2'].value + ws['B1'].value 
print(ws['C1'].value)

我得到了一个字符串结果,它连接了这两个 cell.value:

str format
[output:]
= A1+A2= A2*A4

我知道一些方法可以通过使用一些第 3 方库来获取单元格的真实值而不是函数或方程式,例如 pandas

但我想搜索一种方法,只需使用 openpyxl

使用 OpenPyXl 使用实际值而不是公式进行操作的唯一方法是使用

打开该工作簿

load_workbook(xl_path, data_only = True)

这将打开完全相同的工作簿,但所有单元格将只有它们的计算值。

Read Excel cell value and not the formula computing it -openpyxl