Python XlsxWriter - Write to many sheets

2019-07-18 02:04发布

问题:

This writes to the first sheet but all other sheets are blank.

for date in dates:
    sheet = wb.add_worksheet(date)
    sheet.write(row, 0, 'example')

For each date I want to creat a sheet with the name equal to the date and then write to each sheet. The sheets are created with the correct names but there is nothing in them except for the first sheet.

回答1:

I supposed that your variable wb is the workbook and is initialized before. In the instruction

sheet.write(row, 0, 'example')

I don't know how row is enhanced. So i created a little script:

import xlsxwriter
list_name = ["first sheet", "second sheet", "third sheet"]

workbook = xlsxwriter.Workbook(<Your full path>)
for sheet_name in list_name:
    worksheet = workbook.add_worksheet(sheet_name)
    worksheet.write('A1', sheet_name)

workbook.close()

Regards



回答2:

This is the code snippet which is working fine for me.you can replace the normal integers in the list with the dates.It is creating multiple sheets and writing into all sheets also.

import xlwt
List=[1,2,3,4]


book = xlwt.Workbook(encoding = "utf-8")

for i in List:  
    sheet1=book.add_sheet(str(i))
    sheet1.write(0,0,"Example")
book.save("Test_Status.xls")