Reputation: 137
I'm trying to create multiple sheets based on the values in different rows in column 1 using openpyxl. The thing is when I run my script, it only just creates a new sheet using the first value and that's it.
How can I create multiple sheets using different values available in "Sheet1"?
My attempt so far:
from openpyxl import load_workbook
wb = load_workbook('Document.xlsx')
ws = wb['Sheet1']
if __name__ == '__main__':
for row in range(2, ws.max_row +1):
if ws.cell(row=row,column=1).value==None:break
keyword = ws.cell(row=row,column=1).value
ws = wb.create_sheet(str(keyword)) #defining ws is a must here in order to reuse later
print(keyword)
wb.save('Document.xlsx')
These are the inputs available in Sheet1
for creating three different sheets:
45824
33125
13958
Upvotes: 0
Views: 1180
Reputation: 837
You are overwriting the ws
variable and therefore creating a None value in ws.cell(row=row,column=1).value
. Changing ws = wb.create_sheet(str(keyword))
to ws2 = wb.create_sheet(str(keyword))
solves your issue.
Upvotes: 1