biddls
biddls

Reputation: 41

how to write an array into an XLSX sheet using openpyxl

def save_data():
    wb = openpyxl.Workbook()
    print(entryArray)
    for b in range(len(entryArray)):
        temp = entryArray[b]
        wb.cell(row=b+1, column=1).value = temp
    wb.save('/home/'+getpass.getuser()+'/Desktop/FileName.xlsx')

i get the error message

  File "C:/Users/thoma/ownCloud/Computer Science/Project/Psudo code/1.2/Project 404.py", line 292, in <module>
    save_data()
  File "C:/Users/thoma/ownCloud/Computer Science/Project/Psudo code/1.2/Project 404.py", line 258, in save_data
    wb.cell(row=b+1, column=1).value = temp
AttributeError: 'Workbook' object has no attribute 'cell'

this is the value of entryArray: ['58578', '876', '66', '98', '807', '78907', '90', '078907', '8967', '760', '658', '467', '58346', '54', '65', '785', '896', '9-08', '-980', '456', '456', '09', '87', '89', '765', '765', '765', '765']

how do I create an XLSX sheet then write these values into it using openpyxl. the XLSX sheet does not exist I need to create it during the functions running.

thanks in advance

Upvotes: 2

Views: 9854

Answers (2)

Prove that:

r=entryArray.shape

for i in range(r[0]):

    for j in range(r[1]):

        ws.cell(row=i+2, column=j+1).value = entryArray[i][j]

Upvotes: 0

Yilun Zhang
Yilun Zhang

Reputation: 9018

The problem is that you are accessing your cells from workbook which does not have cell attribute. What you should access before then is the worksheet object. You can do that via:

ws = wb.active

So a simple fix is to add the code above right after your wb = ...; and change the wb to ws within your loop. This will give you a local excel file with your data in the first column:

enter image description here

Upvotes: 1

Related Questions