Openpyxl:'Worksheet'对象没有属性'值' [英] Openpyxl: 'Worksheet' object has no attribute 'values'

查看:4304
本文介绍了Openpyxl:'Worksheet'对象没有属性'值'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是读取一个excel文件,并查看大熊猫数据帧(即'= A3')中的代码,而不是从执行代码执行代码的结果值(如果使用熊猫读取的话,这是大熊猫默认值)。 / p>

我的目标描述如下:如何在大熊猫/ python中查看excel电子表格的公式?



Openpyxl应该支持这个,但是我无法使导入功能正常。任何人发现错误?

 从openpyxl导入load_workbook 
从openpyxl.utils导入pandas作为pd
。 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'对象没有属性'value'

(注意:答案的确切实现链接的问题产生 KeyError:'Worksheet范围名称不存在'。我的代码解决了这个问题,但是如上所述被卡住。)

$ b $



openpyxl 2.4.2

  import openpyxl 
print(openpyxl .__ version__)

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


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.

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

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

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.)

解决方案

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

openpyxl 2.4.2

import openpyxl
print(openpyxl.__version__)

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

这篇关于Openpyxl:'Worksheet'对象没有属性'值'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆