Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a sheet to an existing excel worksheet without deleting other sheet

I am trying to add a sheet to the excel file: ex.xls and whenever I do it deletes all the previously made sheets.

How do I add a sheet to this excel file without deleting the other sheets?

Here is my code to create a sheet:

import xlwt
import xlrd

wb = Workbook()
Sheet1 = wb.add_sheet('Sheet1')
wb.save('ex.xls')
like image 492
Nick M. Avatar asked Dec 25 '22 04:12

Nick M.


1 Answers

I believe this is the only way to do what you want:

import xlrd, xlwt
from xlutils.copy import copy as xl_copy

# open existing workbook
rb = xlrd.open_workbook('ex.xls', formatting_info=True)
# make a copy of it
wb = xl_copy(rb)
# add sheet to workbook with existing sheets
Sheet1 = wb.add_sheet('Sheet1')
wb.save('ex.xls')
like image 120
mechanical_meat Avatar answered Dec 27 '22 03:12

mechanical_meat