-2

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")
Rahul
  • 1
  • 1
  • 3

2 Answers2

0

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())
Community
  • 1
  • 1
bereal
  • 32,519
  • 6
  • 58
  • 104
0

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)
user3684792
  • 2,542
  • 2
  • 18
  • 23