writing to existing workbook using xlwt [closed]
Here's some sample code I used recently to do just that.
It opens a workbook, goes down the rows, if a condition is met it writes some data in the row. Finally it saves the modified file.
from xlutils.copy import copy # http://pypi.python.org/pypi/xlutilsfrom xlrd import open_workbook # http://pypi.python.org/pypi/xlrdSTART_ROW = 297 # 0 based (subtract 1 from excel row number)col_age_november = 1col_summer1 = 2col_fall1 = 3rb = open_workbook(file_path,formatting_info=True)r_sheet = rb.sheet_by_index(0) # read only copy to introspect the filewb = copy(rb) # a writable copy (I can't read values out of this, only write to it)w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copyfor row_index in range(START_ROW, r_sheet.nrows): age_nov = r_sheet.cell(row_index, col_age_november).value if age_nov == 3: #If 3, then Combo I 3-4 year old for both summer1 and fall1 w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old') w_sheet.write(row_index, col_fall1, 'Combo I 3-4 year old')wb.save(file_path + '.out' + os.path.splitext(file_path)[-1])
You need xlutils.copy
. Try something like this:
from xlutils.copy import copyw = copy('book1.xls')w.get_sheet(0).write(0,0,"foo")w.save('book2.xls')
Keep in mind you can't overwrite cells by default as noted in this question.