I wanted to create with input function to create tables and multiple sheets, but it only puts data in last sheet. What to do to put data in every sheet, except from first called 'description' sheet.
import xlsxwriter
from xlsxwriter import workbook
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet_description = workbook.add_worksheet( "description" )
sheetnames = input("Enter (sheets) names: ").split(", ")
for i in sheetnames:
worksheet_data = workbook.add_worksheet(i)
worksheet_data.write('A1', 'Number of table rows')
data = []
year = input("Enter years: ").split(", ")
q = ['q1 ' , 'q2 ', 'q3 ', 'q4 ']
for x in year:
for y in q:
data.append(x + y)
worksheet_data.write_row(0, 1, data)
col = input("Numerate columns (enter value): ").split(", ")
for i in col:
worksheet_data.write_column(1, 0, col)
workbook.close()
Why it doesn't print input data in all worksheet_data
?
If you want to write on each worksheet, you should keep the program working on a specified sheet. On your code, the for
loop is already done, so the program only print your data on the last sheet that active.
Here is my script to print it in each sheet except description.
import xlsxwriter
from xlsxwriter import workbook
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet_description = workbook.add_worksheet( "description" )
sheetnames = input("Enter (sheets) names: ").split(", ")
year = input("Enter years: ").split(", ")
col = input("Numerate columns (enter value): ").split(", ")
for i in sheetnames: # Loop for each sheet
worksheet_data = workbook.add_worksheet(i)
worksheet_data.write('A1', 'Number of table rows')
data = []
q = ['q1 ' , 'q2 ', 'q3 ', 'q4 ']
for x in year:
for y in q:
data.append(x + y)
worksheet_data.write_row(0, 1, data)
for i in col:
worksheet_data.write_column(1, 0, col)
workbook.close()