Reiion
Reiion

Reputation: 943

Python - TypeError: 'Cell' object is not iterable

What I'm trying to do is basically to write a new excel file from the data that I get from a list. The contents of the list is the row contents that I'm trying to write in the new excel file using the xlsxwriter (specifically xlsx because I'm using xlsx). Assuming that I have the code snippet below, it yields me the error :

TypeError: 'Cell' object is not iterable

The whole stacktrace points this out during the write event.

Traceback (most recent call last):
File "Desktop/excel-copy.py", line 33, in <module>
    sheet.write_row(row_index, col_index, cell_value)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 64, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 989, in write_row
    for token in data:
TypeError: 'Cell' object is not iterable


import xlrd
import xlsxwriter

new_workbook = xlsxwriter.Workbook()
sheet = new_workbook.add_worksheet('stops')

#copy all row and column contents to new worksheet
for row_index, row in enumerate(ordered_list_stops):
    for col_index, cell_value in enumerate(row):
        print("WRITING: " + str(cell_value) + "AT " + str(row_index)+ " " + str(col_index))
        sheet.write_row(row_index, col_index, cell_value)

new_workbook.save('output.xlsx')

I can't quite point out whether cell_value is the cause. I tried printing it out and this is the result:

WRITING: text:u'4977'AT 0 0

Upvotes: 1

Views: 8793

Answers (1)

cco
cco

Reputation: 6281

The problem is that write_row takes a list (or other iterable) of values, and you're passing a single Cell object (cell_value) instead.

You either want to use sheet.write(row_index, col_index, cell_value), or skip the inner for loop and use sheet.write_row(row_index, 0, row)

Upvotes: 2

Related Questions