AttributeError: 'Sheet' object has no attribute 'write'

Peter Lazarov picture Peter Lazarov · Jan 21, 2014 · Viewed 23k times · Source

I am trying to write a string in a cell within an excel file. My code is

import xlwt
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_index(0)
worksheet.write(0,2,"string")

While I was looking for a solution I leardned that it could be becouse my xlwt library has an old version. However when I checkied it I got xlwt: 0.7.5. And I was once again left clueless. Any help is appreciated.

Answer

Peter Lazarov picture Peter Lazarov · Jan 21, 2014

After Looking into the problem I found a solution using the xlwt library to write the data on a virtual workbook and the xlutils library to save it and thus make the virtual workbook into an actual .xls file.

import xlrd
import xlwt
from xlutils.copy import copy
import os.path
rb = xlrd.open_workbook('my_workbook.xls',formatting_info=True)
r_sheet = rb.sheet_by_index(0) 
wb = copy(rb) 
sheet = wb.get_sheet(0) 
sheet.write(5,2,"string")
wb.save('my_workbook.xls')