2017-07-20 42 views
0

我想讀取一個Excel工作簿到一個三維數組([工作表] [列] [單元格]),但我得到一個錯誤與openpyxl(v2.5.0a2),看起來像它與在線文檔相矛盾。AttributeError與openpyxl

工作表模塊的文檔明確指出有'列'屬性(我已經看過使用它的例子),但是我得到一個「AttributeError:'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' 
+0

[編輯]你的問題,並顯示你如何打開工作簿,以及完整的Traceback。錯誤告訴你打開它ReadOnly:_ **「AttributeError:'ReadOnlyWorksheet'** _ – stovfl

回答

0

解決它,以供將來參考它看起來並不像「ReadOnlyWorksheet」對象確實含有'列'屬性(這很奇怪)。刪除load_workbook的只讀參數解決了這個問題。