0

Firstly thanks for willing to answer my question. I have a list include [12A, 23B, 34C, 45D, ...], there will be 500 team names in the list, the string teamloop keeps changing every time running the loop of creating a sheet.

Is there any way to use a loop to create new sheets, just like:

sheet1 = book.add_sheet(teamloop, cell_overwrite_ok=True)
sheet2 = book.add_sheet(teamloop, cell_overwrite_ok=True)
sheet3 = book.add_sheet(teamloop, cell_overwrite_ok=True)
sheet4 = book.add_sheet(teamloop, cell_overwrite_ok=True)
sheet5 ...
...

This is the loop: ↓

while number < 4:
    teamloop = list1[number]
    sheet2 = book.add_sheet(teamloop, cell_overwrite_ok=True)
    number += 1

So basically, the question is how to increase the number after sheet in a loop.

Thanks!

YINGFENG
  • 39
  • 6
  • @Aran-Fey I don't think so, what I am asking is if I can use a loop to do sheet1 sheet2 sheet3 and I tried sheet+number.write() and it is not working. – YINGFENG Apr 10 '18 at 23:02
  • Yes, the answer is store your sheets in a dict. `sheets['sheet'+str(number)] = book.add_sheet(...)`. Or in this case, just store them in a list: `sheets.append(book.add_sheet(...))`. Whenever you have more than 3 variables with numbers at the end, you're doing something wrong and should be using a list/dict instead. – Aran-Fey Apr 10 '18 at 23:03
  • @Aran-Fey Thanks, solved my issue. Should I delete this post? – YINGFENG Apr 10 '18 at 23:09
  • Either that or mark it as a duplicate (there should be a "This solved my problem" button that you can click, I think). Your choice. – Aran-Fey Apr 10 '18 at 23:11
  • @Aran-Fey There is actually an error. `['sheet' + str(number)].write = book.add_sheet(teamloop, cell_overwrite_ok= True) AttributeError: 'list' object has no attribute 'write'`. Please check when you have time. – YINGFENG Apr 10 '18 at 23:32

0 Answers0