TypeError: Value must be a list, tuple, range or generator, or a dict

11k Views Asked by At

I have created a script to extract XML values via regular expressions and then append to an Excel sheet. I then added a element tree method to grab a value by the tag name. I also want to append that value to the same XLSX sheet.

I am getting this error:

Traceback (most recent call last):
  File "C:\Path\To\Desktop\excels.py", line 23, in <module>
    ws.append(agreement)
  File "C:\Python34\lib\site-packages\openpyxl\worksheet\worksheet.py", line 763, in append
    self._invalid_row(iterable)
  File "C:\Python34\lib\site-packages\openpyxl\worksheet\worksheet.py", line 768, in _invalid_row
    type(iterable))
TypeError: Value must be a list, tuple, range or generator, or a dict. Supplied value is <class 'str'>

This is the example elementTree code that I found and am testing.

from openpyxl import Workbook
import xml.etree.ElementTree as ET

xmlfile = 'test.xml'
element_tree = ET.parse(xmlfile)
root = element_tree.getroot()
agreement = root.find(".//tag").text
#variable = int(agreement)

print (agreement)


wb = Workbook()
kevin = ["1", "2", "3"]

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append(agreement)
ws.append(kevin)


# Save the file
wb.save("sample.xlsx")
1

There are 1 best solutions below

3
On BEST ANSWER

append() function on a WorkSheet instance receives an iterable as an argument, while you are passing in a string (agreement variable).

Instead, you meant something like:

ws.append([42, agreement])

or:

ws.append([agreement])