I need to write to multiple sheets with sheets name stored in a list.
Below is my code
for row_num, obj in enumerate(list,1):
sheet = workbook.add_worksheet(obj.Attribute1)
sheet.write(row_num, 0, obj.Attr1)
sheet.write(row_num, 1, obj.Attr2)
sheet.write(row_num, 2, obj.Attr3)
....
For each object in list i want to create a sheet. Above code is creating multiple sheets with desired name but data is only present in the first sheet.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It should work as expected.
Here is your sample code wrapped into a working example:
import xlsxwriter
class MyObj:
def __init__(self, id):
self.Attribute1 = 'Sheet%s' % id
self.Attr1 = 'Attr1'
self.Attr2 = 'Attr2'
self.Attr3 = 'Attr3'
obj_list = []
for id in range(1, 4):
my_obj = MyObj(id)
obj_list.append(my_obj)
workbook = xlsxwriter.Workbook('test.xlsx')
for row_num, obj in enumerate(obj_list, 1):
sheet = workbook.add_worksheet(obj.Attribute1)
sheet.write(row_num, 0, obj.Attr1)
sheet.write(row_num, 1, obj.Attr2)
sheet.write(row_num, 2, obj.Attr3)
workbook.close()
Output:
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
