Python Forum

Full Version: excel data erase
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to write new data to excel, however, this code erases the old one.
Is there a way to slightly modify this code without erasing the old data ?

import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet 1')

listdata = ['write', 'list', 'to', 'excel', 'row']

first_row = 0

# write each item in the list to consecutive columns on the first row
for index, item in enumerate(listdata):
        ws.write(first_row, index, item) 

wb.save('example.xls')
Looks like have to modify a copy with xlutils,then write to a new document with those changes.
from xlutils.copy import copy
from xlrd import open_workbook

w = copy(open_workbook('example.xls'))
w.get_sheet(0).write(0, 0, "foo")
w.save('example_1.xls')
I use Pandas always for stuff like this df.read_excel(),
when in a DataFrame(look like Excel in a NoteBook) then the power is great for all kind of stuff.
When finish with changes df.to_excel()