我正在尝试将Excel工作簿读入三维数组([worksheet] [column] [cell]),但是openpyxl(v2.5.0a2)出现错误,看起来与在线文档相矛盾。

工作表模块的文档明确指出有一个“列”属性(并且我已经看到了使用它的示例),但是我收到一个“ AttributeError:'ReadOnlyWorksheet'对象没有属性'columns'”错误。

下面的代码,有什么线索吗?

# 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]


它产生的错误在cells_by_column行上生成,并读取...

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'

最佳答案

解决了它,以供将来参考,它看起来不像'ReadOnlyWorksheet'对象包含'column'属性(这很奇怪)。删除load_workbook的只读参数即可解决此问题。

09-20 09:35