openpyxl 的属性错误
AttributeError with openpyxl
我正在尝试将 Excel 工作簿读入三维数组 ([worksheet][column][cell]),但我在使用 openpyxl (v2.5.0a2) 时遇到错误这看起来与在线文档相矛盾。
工作表模块的文档清楚地指出有一个 'columns' 属性(我已经看到使用它的示例),但我得到了一个 "AttributeError: 'ReadOnlyWorksheet' object has no attribute '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 的只读参数解决了这个问题。
我正在尝试将 Excel 工作簿读入三维数组 ([worksheet][column][cell]),但我在使用 openpyxl (v2.5.0a2) 时遇到错误这看起来与在线文档相矛盾。
工作表模块的文档清楚地指出有一个 'columns' 属性(我已经看到使用它的示例),但我得到了一个 "AttributeError: 'ReadOnlyWorksheet' object has no attribute '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 的只读参数解决了这个问题。