cell (1,1) should have "Put me in same cell":
import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet('Sheet1')
sheet.write(1, 1, "Put me")
n=10
if n>0:
sheet.write(1, 1, "in same cell")
cell (1,1) should have "Put me in same cell":
import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet('Sheet1')
sheet.write(1, 1, "Put me")
n=10
if n>0:
sheet.write(1, 1, "in same cell")
Thats the default xlwt behavior, you can either override that with
sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)
(taken from this answer)
That will only overwrite the original value Put me
, though.
You'll have to merge the values manually, like this:
from cStringIO import StringIO
sheet = book.add_sheet('Sheet1')
buf = StringIO()
buf.write('Put me')
if some_condition:
buf.write(' in same cell')
sheet.write(1, 1, buf.getvalue())
try openpyxl, this works for me:
from openpyxl import Workbook
def append_to_cell(location,text,sheet):
new = sheet.cell(location).value+text
sheet.cell(location).value=new
return
file_name='example.xlsx'
cell='A1'
wb = Workbook()
ws = wb.create_sheet()
ws.title = 'test Sheet'
ws.cell(cell).value = 'write this '
append_to_cell(cell,'in same cell',ws)
wb.save(filename=file_name)