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/xlutils
from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
START_ROW = 297 # 0 based (subtract 1 from excel row number)
col_age_november = 1
col_summer1 = 2
col_fall1 = 3
rb = open_workbook(file_path,formatting_info=True)
r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
wb = 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 copy
for 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])
Answer from Greg on Stack OverflowHere'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/xlutils
from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
START_ROW = 297 # 0 based (subtract 1 from excel row number)
col_age_november = 1
col_summer1 = 2
col_fall1 = 3
rb = open_workbook(file_path,formatting_info=True)
r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
wb = 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 copy
for 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 copy
w = 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.
python 3.x - How do I write to an existing excel file using xlwt and keep the formatting? - Stack Overflow
python - xlwt write excel sheet on the fly - Stack Overflow
xlw - python xlwt, write to the next available line - Stack Overflow
python - Writing multi-line strings to cells using xlwt module - Stack Overflow
Should I still use xlwt to write Excel files?
openpyxl vs XlsxWriter - which should I choose?
How do I make Excel open my CSV with the correct accented characters?
To quote the documentation for the .save() method of xlwt:
It can also be a stream object with a write method, such as a
StringIO, in which case the data for the excel file is written to the stream.
Modified example:
from io import StringIO # instead of Python 2.x `import StringIO`
f = StringIO() # create a file-like object
wbk = xlwt.Workbook()
earnings_tab = wbk.add_sheet('EARNINGS')
wbk.save(f) # write to stdout
Some may suggest you use cStringIO instead of StringIO, but be forewarned that cStringIO when last I checked does not properly handle Unicode.
Important Update
It's perhaps also worth noting that StringIO is replaced in Python 3 by io.
The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.
source
So use:
from io import StringIO
# instead of import StringIO
this is what i use in Django:
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=file.xls'
book.save(response)
return response
I found the answer in the python-excel Google Group. Using sheet.write() with the optional style argument, enabling word wrap for the cell, does the trick. Here is a minimum working example:
import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet('Test')
# A1: no style, no wrap, despite newline
sheet.write(0, 0, 'Hello\nWorld')
# B1: with style, there is wrap
style = xlwt.XFStyle()
style.alignment.wrap = 1
sheet.write(0, 1, 'Hello\nWorld', style)
book.save('test.xls')
While in cell A1 shows HelloWorld without linebreak, cell B1 shows Hello\nWorld (i.e. with linebreak).
If you don't use XFStyle and instead easyxf it's done like this:
import xlwt
style_head = xlwt.easyxf('alignment: wrap True')
row = 1
cell = 1
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet()
sheet.write(row, cell, 'cell value', style_head)