Michael Hillman
Michael Hillman

Reputation: 1287

AttributeError with openpyxl

I'm trying to read an Excel workbook into a three-dimensional array ([worksheet][column][cell]) but I'm getting an error with openpyxl (v2.5.0a2) that looks like it contradicts the online documentation.

The documentation for the worksheet module clearly states that there's a 'columns' attribute (and I've seen examples using it), but I get an "AttributeError: 'ReadOnlyWorksheet' object has no attribute 'columns'" error.

Code below, any clues?

# Load spreadsheet in read only mode
wb = load_workbook(filename=input_file, read_only=True)

# Three Dimensional array of every sheet, then every row, then every value
cells_by_row=[[[cell.value for cell in row if cell.value is not None] for row in sheet.rows] for sheet in wb.worksheets]

# Three Dimensional array of every sheet, then every column, then every value
 cells_by_column=[[[cell.value for cell in column if cell.value is not None] for column in sheet.columns] for sheet in wb.worksheets]

The error it produces is generated on the cells_by_column line, and reads...

Traceback (most recent call last):
File "D:\Repositories\interpolator\rate_shape_map_interpolator.py", line 293, in <module>
Result = interpolator(RailPressure, FuelQuantity, RPM, SOI, InputMap, InputMode, ImmediateSOI, Density)
File "D:\Repositories\interpolator\rate_shape_map_interpolator.py", line 86, in interpolator
cells_by_column=[[[cell.value for cell in column if cell.value is not None] for column in sheet.columns] for sheet in wb.worksheets]
File "D:\Repositories\interpolator\rate_shape_map_interpolator.py", line 86, in <listcomp>
cells_by_column=[[[cell.value for cell in column if cell.value is not None] for column in sheet.columns] for sheet in wb.worksheets]
AttributeError: 'ReadOnlyWorksheet' object has no attribute 'columns'

Upvotes: 5

Views: 9876

Answers (2)

Hafiz Muhammad Shafiq
Hafiz Muhammad Shafiq

Reputation: 8670

Little elaboration. Changing to False fix my issue

Old

openpyxl.load_workbook(filename=self.students_file,read_only=True)

New

openpyxl.load_workbook(filename=self.students_file,read_only=False)

Upvotes: 0

Michael Hillman
Michael Hillman

Reputation: 1287

Solved it, for future reference it doesn't look like the 'ReadOnlyWorksheet' object does contain a 'column' attribute (which is strange). Removing the read only argument to load_workbook solved the issue.

Upvotes: 10

Related Questions