简体   繁体   中英

openpyxl error on the linux machine

I have been previously running the code fine but am getting this error all of a sudden with the openpyxl:

>>>from openpyxl import load_workbook
>>>wb = load_workbook("wb.xlsx")
test!$A$2 True
test!$A$3 True
>>> ws = wb['sheet']
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'Workbook' object has no attribute '__getitem__'

test in the name of one of the sheets in my xlsx file.

I ran the same code on linux and mac. Previously the code was working fine in both the environments but now the code is breaking in the linux environment, not sure why.

I have the same problem...

The very odd part, is that it went away when I was using ipython, and then came back.

Here is the code (note 'example.xlsx' can be any spreadsheet with A1 filled.)

import openpyxl
#D
arg1 = "A1"
'''Reads in a sheet'''
wb = openpyxl.load_workbook('example.xlsx')
ws =  "".join(wb.get_sheet_names()[0])
sheet_ranges = wb[ws]
print(sheet_ranges[arg1].value)

The result is we fail at this line:

sheet_ranges = wb[ws]

This is the result:

TypeError: 'Workbook' object has no attribute ' getitem '

I suspect that it worked for a while because I added some magic line...

Here is a clue:

dir(openpyxl. doc ) [' add ', ' class ', ' contains ', ' delattr ', ' doc ', ' eq ', ' format ', ' ge ', ' getattribute ', ' getitem ', ' getnewargs ', ' getslice ', ' gt ', ' hash ', ' init ', ' le ', ' len ', ' lt ', ' mod ', ' mul ', ' ne ', ' new ', ' reduce ', ' reduce_ex ', ' repr ', ' rmod ', ' rmul ', ' setattr ', ' sizeof ', ' str ', ' subclasshook ', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Note that there is documentation for getitem

TADA

The answer is that the openpyxl version I had loaded via the debian repository was version 1.5. You discover this horrible fact using the following code:

''' checks version of openpyxl '''
if str(openpyxl.__version__) <> "2.3.2":
#T if str(openpyxl.__version__) <> "1.5":
    print "Trouble Ahead...this machine uses openpyx version "+str(openpyxl.__version__)
else:
    print "Good to Go! This this machine uses openpyx openpyx version "+str(openpyxl.__version__)

The current version is 2.3.2. The older version will make you cry.

Flint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM