Python 带有openpyxl的AttributeError

Python 带有openpyxl的AttributeError,python,openpyxl,Python,Openpyxl,我试图将Excel工作簿读入三维数组([工作表][列][单元格]),但openpyxl(v2.5.0a2)出现了一个错误,看起来它与在线文档相矛盾 工作表模块的文档清楚地说明了有一个“columns”属性(我见过使用它的示例),但我得到了一个“AttributeError:'ReadOnlyWorksheet'对象没有属性“columns'”错误 下面的代码,有什么线索吗 # Load spreadsheet in read only mode wb = load_workbook(filena

我试图将Excel工作簿读入三维数组([工作表][列][单元格]),但openpyxl(v2.5.0a2)出现了一个错误,看起来它与在线文档相矛盾

工作表模块的文档清楚地说明了有一个“columns”属性(我见过使用它的示例),但我得到了一个“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_列上生成的,并读取

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'
回溯(最近一次呼叫最后一次):
文件“D:\Repositories\interpolator\rate\u shape\u map\u interpolator.py”,第293行,在
结果=内插器(轨道压力、燃油量、转速、SOI、输入映射、输入模式、即时、密度)
文件“D:\Repositories\interpolator\rate\u shape\u map\u interpolator.py”,第86行,插入器中
cells_by_column=[[[cell.value对于column中的cell,如果cell.value不是None]对于sheet.columns中的column]对于wb.worksheets中的sheet]
文件“D:\Repositories\interpolator\rate\u shape\u map\u interpolator.py”,第86行,中
cells_by_column=[[[cell.value对于column中的cell,如果cell.value不是None]对于sheet.columns中的column]对于wb.worksheets中的sheet]
AttributeError:“ReadOnlyWorksheet”对象没有属性“columns”

解决了这个问题,为了将来参考,它看起来不像“ReadOnlyWorksheet”对象包含“column”属性(这很奇怪)。删除只读参数以加载工作簿解决了此问题。

您的问题并演示如何打开工作簿以及完整的回溯。错误表明您以只读方式打开了它:“AttributeError:‘ReadOnlyWorksheet’”感谢您在2019年帮助我!