pandas Openpyxl:“工作表”对象没有属性“值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42397769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:02:31  来源:igfitidea点击:

Openpyxl: 'Worksheet' object has no attribute 'values'

pythonexcelpython-3.xpandasopenpyxl

提问by ThrowAway23948238

My goal is to read in an excel file and view the codes in a pandas dataframe (i.e. '= A3') rather than the resulting values from excel executing the codes, which is the pandas default if read in using pandas.

我的目标是读入 excel 文件并查看 Pandas 数据框中的代码(即“= A3”),而不是 excel 执行代码的结果值,如果使用 Pandas 读入,这是 Pandas 的默认值。

My goal was described here: How can I see the formulas of an excel spreadsheet in pandas / python?

我的目标在这里描述:如何在 Pandas/python 中查看 excel 电子表格的公式?

Openpyxl is supposed to support this, but I can't get the import to function correctly. Anyone spot the error?

Openpyxl 应该支持这一点,但我无法让导入正常运行。有人发现错误吗?

import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows

df = pd.DataFrame()
wb = load_workbook(filename = 'name.xlsx')
sheet_names = wb.get_sheet_names()
name = sheet_names[0]
sheet_ranges = wb[name]
df = pd.DataFrame(sheet_ranges.values)

> AttributeError: 'Worksheet' object has no attribute 'values'

(Note: the exact implementation of the answer at the linked question yields KeyError: 'Worksheet range names does not exist.'My code above resolved this, but then gets stuck as described.)

(注意:链接问题中答案的确切实现产生了KeyError: 'Worksheet range names does not exist.'我上面的代码解决了这个问题,但随后如描述的那样卡住了。)

回答by Shijo

Check your version of openpyxl, It seems you have an older version.

检查您的 openpyxl 版本,您的版本似乎较旧。

openpyxl 2.4.2

openpyxl 2.4.2

import openpyxl
print(openpyxl.__version__)

Values property for worksheets were added only from 2.4.0-a1 (2016-04-11)

工作表的值属性仅从2.4.0-a1 (2016-04-11) 添加