openpyxl的AttributeError [英] AttributeError with openpyxl

查看:140
本文介绍了openpyxl的AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to read an Excel workbook into a three-dimensional array ([worksheet][column][cell]) but I'm getting an error with openpyxl (v2.5.0a2) that looks like it contradicts the online documentation.

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

The documentation for the worksheet module clearly states that there's a 'columns' attribute (and I've seen examples using it), but I get an "AttributeError: 'ReadOnlyWorksheet' object has no attribute 'columns'" error.

下面的代码,有任何线索吗?

Code below, any clues?

# 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行上生成,并读取...

The error it produces is generated on the cells_by_column line, and reads...

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的只读参数即可解决此问题.

Solved it, for future reference it doesn't look like the 'ReadOnlyWorksheet' object does contain a 'column' attribute (which is strange). Removing the read only argument to load_workbook solved the issue.

这篇关于openpyxl的AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆