ThrowAway23948238
ThrowAway23948238

Reputation: 133

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

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

Upvotes: 0

Views: 15999

Answers (1)

Shijo
Shijo

Reputation: 9711

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)

Upvotes: 3

Related Questions