Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
excel data erase
#1
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')
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,045 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
  Help with poorly formatted excel data armitron121 1 1,721 Jan-13-2022, 07:31 AM
Last Post: paul18fr
  Exporting data frame to excel dyerlee91 0 1,629 Oct-05-2021, 11:34 AM
Last Post: dyerlee91
  [Pandas] Write data to Excel with dot decimals manonB 1 5,875 May-05-2021, 05:28 PM
Last Post: ibreeden
  Python read Excel cell data validation anantpatil 0 4,165 Jan-31-2020, 04:57 PM
Last Post: anantpatil
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 6,115 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  Aligning excel data gat 1 2,206 Jun-17-2019, 07:05 PM
Last Post: michalmonday
  Copy raw data in excel to another new excel file keerthiprashanth 5 3,889 Oct-20-2018, 10:13 AM
Last Post: volcano63
  I'm working onn below code to extract data from excel using python kiran 1 3,279 Oct-24-2017, 01:42 PM
Last Post: kiran
  Write data into existing Excel (xlsx) file with multiple sheets BNB 1 15,342 Jun-01-2017, 04:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020