Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my openpyxl WorkBook.active() give me a TypeError: Object is not callable?

I am trying out openpyxl for the first time. I have an excel file, with two columns of data. Column 'A' has ID-numbers, while column 'C' has values. I am simply trying to extract them, as a plain key/value object.

These are the pseudo steps I figure i need to implement

  • Find the file by location, and create a workbook object
  • Define a WorkSheet object. In my case, this is the default first page
  • Iterate through the rows, as long as there are key values in column 'A'
  • Add key and value from 'A' and 'C' into an object.

The problem is that I can't get past step 2, creating a worksheet. The line ws = wb.active()seems like a completely standardized way of getting the default WorkSheet. I see it used in a lot of examples, and as of writing this, there are 93 questions on SO using the line. But in my case, it throws an error:

  File "C:\myworkspace\Myclass.py", line 18, in <module>
    ws = wb.active()
TypeError: 'ReadOnlyWorksheet' object is not callable

I am quite new to Python, but I've run into similar errors before. I know that it is thrown for instance if I import a python module name, instead of the relevant class name. What exactly is happening here? Why is this seemingly standard method of retrieving a worksheet object, trying to instanciate a module instead of a class? Am I using it wrong?

file_url = 'C:/myfilepath.xlsx'
key_column_index = 1
value_column_index = 3
row_start = 2

request_map = {}

wb = load_workbook(filename = file_url, use_iterators = True)
ws = wb.active()

row_counter = row_start

while( ws.cell(row=row_counter, column=key_column_index).value ):
    key = ws.cell(row=row_counter, column=key_column_index).value
    value = ws.cell(row=row_counter, column=value_column_index).value
    request_map[key] = value

    row_counter += 1

pprint(dict([(n, tuple(l.split(';'))) for n, l in enumerate(requests.split('\n'))]))

Just to make this really really weird, I present you with this:

I wanted to reproduce my error in the simplest possible way, so I found the shortest example I could, and copied it into my workspace. With the exception of the file path, this is completely identical to Ryu_Hayabusa's answer to this question: parsing excel documents with python. Now get this: It works, without any errors!

file_url = 'C:/myfilepath.xlsx'
wb = load_workbook(file_url)
ws = wb.active
for row in ws.iter_rows():
   for cell in row:
     print cell.value

Of course, i suspected that the bold text in the line

wb = load_workbook(filename = file_url , use_iterators = True )

was the problem, but no. Removing the bold marked text, to make it identical to the working example, still produced an error. The only difference is that now it says Worksheet instead of ReadOnlyWorksheet What in the big wide world is going on here???

like image 672
jumps4fun Avatar asked Mar 02 '16 13:03

jumps4fun


1 Answers

In openpyxl, wb.active is provided by the library as a property giving you the currently active worksheet. As such you do not need to add () to call it as a function.

ws = wb.active

Your cut down example does it correctly.

like image 177
Martin Evans Avatar answered Oct 18 '22 12:10

Martin Evans