1

我有一个 excel 文件,其中包含会计数据以及某些单元格的文件使用公式。当我使用 pandas read_excel 读取文件中的值时,它会返回nan具有公式的单元格的值。我也使用过openpyxl,但仍然遇到同样的问题。

对于具有公式的单元格,有什么方法可以读取值而不是公式。

我还附上了使用的 xlsx 文件。

https://drive.google.com/file/d/1aOTYwtKTrqyjF16vDizzzvMkUsvdgRe1/view?usp=sharing

谢谢...

4

3 回答 3

2

在使用您的excelsheet之前,请确保您已将excel文件的权限设置为读写,如果它是只读文件,则更改为读写。

import pandas as pd
your_data = pd.read_excel('yourfile.xlsx',sheet_name='your_sheet_name')
print(your_data) #checking
your_data.dropna(inplace=True)
your_data.rename(columns = {'Unnamed: 1':'Total'},inplace =True) 
print(your_data['Total'].tolist()) #The column name where your formula is being calculated.
于 2020-01-21T09:51:37.613 回答
1

i was facing the exact same issue as you when reading an excel file that has been appended to using excel writer. It happens because the links on the excel file is not updated hence the formulas do not calculate the value.

2 solutions that worked for me:

  1. Open the file and update links when prompted OR
  2. Add in code to refresh the file before reading it according to solution posted here. Refresh Excel External Data with Python

Hope this helps.

Edit: New user here, no idea how reject/approve works, any constructive feedback will be greatly appreciated.

于 2020-05-11T08:32:44.693 回答
1

我没有足够的声誉点来发表评论,所以我必须把它作为答案,但事实并非如此。

使用您的示例文件,我没有任何问题。我正在使用 python 3.8 和 Pandas 0.25.3。熊猫读得很好,并且数字可用。也就是说,我阅读由公式计算的值的经验是,它取决于公式的复杂性。像 Index、Match 这样的可变函数将被读取为 nan,但更简单的函数可以读取为数字。

这只是一种解决方法,而不是真正的解决方案,但是如果您可以使用仅引用具有复杂公式的列的公式创建一个新列,则可以读取该列的数据。

于 2020-01-25T00:07:03.270 回答