I am writing data with self-defined type to an excel file by using xlsxwriter, but got an error:
TypeError: Unsupported type <class '__main__.node'> in write()
Code:
import xlsxwriter
class node(object):
def __init__(self, value):
self.value = value
self.children = []
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level+1)
return ret
def add(self, nod):
self.children.append(node(nod))
leaf_1 = ['AA','BB','CC','DD']
workbook = xlsxwriter.Workbook('print_def.xlsx')
worksheet = workbook.add_worksheet()
tree = parent = node(leaf_1[0]) #### code 1
parent.add(leaf_1[1])
parent.add(leaf_1[2])
print(tree)
worksheet.write(1, 0, tree)
Expected result(put the tree with indent spaces in the specific cell):
Question:
Does anyone have experience on this error? or suggestion? Thanks in advance!
Update:
I added the following part after the print(tree), instead of using worksheet.write(1, 0, tree)
def write_node(worksheet, row, col, tree, format = None):
return worksheet.write_string(row, col, str(tree), format)
worksheet.add_write_handler(tree.children, write_node)
# worksheet.add_write_handler(tree.node, write_node)
worksheet.write('A1', tree)
But, got error as follows:
Traceback (most recent call last):
File "/Users.../pythonProject/2021-05-16.py", line 137, in <module>
worksheet.add_write_handler(tree.children, write_node)
File "/Users.../pythonProject/lib/python3.9/site-packages/xlsxwriter/worksheet.py", line 1365, in add_write_handler
self.write_handlers[user_type] = user_function
TypeError: unhashable type: 'list'

