AttributeError with openpyxl

北慕城南 提交于 2019-12-11 01:49:24

问题


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'

回答1:


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.



来源:https://stackoverflow.com/questions/45220078/attributeerror-with-openpyxl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!