带有openpyxl的AttributeError

时间:2017-07-20 16:11:44

标签: python openpyxl

我试图将Excel工作簿读入三维数组([工作表] [列] [单元格]),但我收到的openpyxl(v2.5.0a2)错误它与在线文档相矛盾。

工作表模块的文档清楚地说明了“'列'属性(我已经看过使用它的例子),但我得到一个"属性错误:' ReadOnlyWorksheet'对象没有属性'列'"错误。

下面的代码,任何线索?

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

1 个答案:

答案 0 :(得分:2)

解决了这个问题,为了将来的参考,它看起来并不像“ReadOnlyWorksheet”那样。对象确实包含一个&#39;列&#39;属性(这很奇怪)。删除load_workbook的只读参数解决了这个问题。