How to add a new sheet to the existing workbook whenever the method is called

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to add a new sheet to the existing workbook whenever the method is called



I need to add a new sheet to the existing workbook and write new data on it, whenever the method is called. I checked this answer but still couldn't figure out a solution for this. What happens now is everytime I call the method, existing data get overwritten by new data. My method is as below.


import xlsxwriter

def writeToExcel(pattern, cashList, startDate, endDate):

workbook=xlsxwriter.Workbook('Expenses01.xlsx')
worksheet=workbook.add_worksheet()
bold = workbook.add_format('bold': True)

worksheet.set_column('A:A', 10)
worksheet.set_column('B:C', 20)
worksheet.write('A1', 'Date', bold)
worksheet.write('B1', 'Replenished Amount', bold)
worksheet.write('C1', 'Prediction', bold)
worksheet.write('D1', 'Cost', bold)

date1=startDate
date2=endDate
date1=date1.strftime('%Y-%m-%d')
date2=date2.strftime('%Y-%m-%d')

start=datetime.datetime.strptime(date1, '%Y-%m-%d')
end = datetime.datetime.strptime(date2, '%Y-%m-%d')
#print date1
#print type(date1)


step=datetime.timedelta(days=1)
row=1
col=0
i=0
while start<=end:

print start.date()
print row
date1=start.date()
#print type(date1)
date_format = workbook.add_format('num_format': 'dd/mm/yy')
worksheet.write_datetime(row, col, date1, date_format)
worksheet.write_number(row, col+1, listReplenished[i])
worksheet.write_number(row, col+2, cashCost[i]*100)
row+=1
start+=step
i=i+1
worksheet.write_number(1, col+3, cost)

workbook.close()



How can I add new sheets to this Expenses01.xlsx and write data on that new sheet at each method call. Any suggestion would be really helpful









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard