pythonexcelxlwtxlutils

how to append particular excel cell value using python?


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")

Solution

  • 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())