-2

单元格(1,1)应该有“把我放在同一个单元格”:

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

2 回答 2

0

这是默认的 xlwt 行为,您可以使用

sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)

(取自这个答案)不过,这只会覆盖原始值Put me

您必须手动合并这些值,如下所示:

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())
于 2014-06-18T12:18:11.550 回答
0

试试openpyxl,这对我有用:

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)
于 2014-06-18T13:20:38.433 回答