How to append cell values in openpyxl directly

I’m trying to append cell value using openpyxl, by appending the value directly.

this works:

wb1=load_workbook('test.xlsx')
ws1=wb1.active

testlist=('two','three','four','five')

for i in testlist:
    ws1['A1'].value = ws1['A1'].value +(' ') + i

print(ws1['A1'].value)

A1 has a value of "one", after the loop runs it has "one two three four five"

But is it possible to use the append method directly on the cell value?

for i in testlist:
        ws1.append['A1'].value = i

however this throws an error
"TypeError: ‘method’ object does not support item assignment"

>Solution :

You will need to move the tuple into a string and can add it to cell A1 like this.

wb1=Workbook()
ws1=wb1.active

testlist=('one','two','three','four','five')
myString = ' '.join(map(str, testlist))
myString.strip()
ws1['A1'].value = myString

wb1.save('test1.xlsx')

Leave a Reply