Python XlsxWriter - Write to many sheets

感情迁移 提交于 2019-12-20 03:16:17

问题


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")


来源:https://stackoverflow.com/questions/37522394/python-xlsxwriter-write-to-many-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!