0

I want to create multiple sheets using the input function. I used the split function, but still it only creates one sheet. The idea is not to be restricted by the number of sheets.

import xlsxwriter
from xlsxwriter import workbook

workbook = xlsxwriter.Workbook('doc.xlsx')

sheetnames = input("Enter value: ").split(', ')

sheetss = []
for i in sheetnames:
  sheetss.append(i)
worksheet_data = workbook.add_worksheet(i)

workbook.close()

Any ideas?

2
  • Is the workbook.add_worksheet(i) inside the for loop? Commented Jul 26, 2021 at 12:59
  • no, workbook.add_worksheet(i) is outside the for loop Commented Jul 26, 2021 at 22:21

1 Answer 1

2

Try:

import xlsxwriter
workbook = xlsxwriter.Workbook("test.xlsx")
sheetnames = input("Enter value: ").split(", ")
for i in sheetnames:
    workbook.add_worksheet(i)
workbook.close()

Using split() already creates a list so you don't need another variable to hold the sheetnames.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it is working, but when I add data, only puts it in last sheet. What to do to put same data in every sheet?
@project.py - Depends what kind of data you're adding. See here. You can ask a new question if you can't figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.