How can openpyxl write list data in sheet?

主宰稳场 提交于 2019-11-30 16:27:17

When using ws.append(row) you must insert pass in a whole either as sequence or as a generator. I don't understand your data structure but something like the following should work.

for row in datalist:
    ws.append(row)

If this is not sufficient then please provide more information about the structure of your data.

Alan Hoover

You are only writing the first column of your data (col=0). In order to include all the data, either add an internal loop:

for i in range(row):
    for j in range(col):
       ws_write.append([datalist[i][j]])
wb.save(filename='data.xlsx')

or, write the entire row at a time:

for i in range(row):
   ws_write.append([datalist[i]])
wb.save(filename='data.xlsx')

I do not know that package to make sure that the output syntax is correct.

edit: after looking at this:Insert row into Excel spreadsheet using openpyxl in Python it appears that you need to do something like:

for i in range(row):
    for j in range(col):
       ws_cell(row=i,col=j).value = datalist[i][j]
wb.save(filename='data.xlsx')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!