Edit existing excel workbooks and sheets with xlrd and xlwt Edit existing excel workbooks and sheets with xlrd and xlwt python python

Edit existing excel workbooks and sheets with xlrd and xlwt


As I wrote in the edits of the op, to edit existing excel documents you must use the xlutils module (Thanks Oliver)

Here is the proper way to do it:

#xlrd, xlutils and xlwt modules need to be installed.  #Can be done via pip install <module>from xlrd import open_workbookfrom xlutils.copy import copyrb = open_workbook("names.xls")wb = copy(rb)s = wb.get_sheet(0)s.write(0,0,'A1')wb.save('names.xls')

This replaces the contents of the cell located at a1 in the first sheet of "names.xls" with the text "a1", and then saves the document.


Here's another way of doing the code above using the openpyxl module that's compatible with xlsx. From what I've seen so far, it also keeps formatting.

from openpyxl import load_workbookwb = load_workbook('names.xlsx')ws = wb['SheetName']ws['A1'] = 'A1'wb.save('names.xlsx')