Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading