Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

excel

xlwt

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.

like image 551
Peter Lazarov Avatar asked Jan 20 '14 23:01

Peter Lazarov


1 Answers

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')
like image 53
Peter Lazarov Avatar answered Oct 15 '22 16:10

Peter Lazarov