将列表中的值插入 excel(代码仅附加一个值)Openpyxl

Inserting values from a list into excel(The code only append one value) Openpyxl

我正在尝试将列表中的值插入 excel,我知道我可以使用字典并会这样做,但我想以这种方式从列表中进行操作。该代码附加值但仅附加一个值。例如,列中出现了 Salsa 的值。提前致谢!

import openpyxl
wb = openpyxl.load_workbook("Python_Example.xlsx")
list_of_music=list(sheet.columns)[4] #With this I can loop over the column number 4 cells 
favorite_music= ['Rock','Bachata','Salsa']
for cellObj in list_of_music: 
   for item in favorite_music: 
       cellObj.value = str(item)  

wb.save("Python_Example.xlsx")

查看 openpyxl 文档;它们包含一些很好的基础教程,可以为您提供帮助,尤其是在遍历单元格范围时。 iter_rowsiter_cols 也是非常有用的工具,可以在这里为您提供帮助。一个简单的解决方案包括:

import openpyxl as op

# Create example workbook
wb = op.Workbook()
ws = wb.active
favourite_music = ['Rock','Bachata','Salsa']

for i, music in enumerate(favourite_music):
    ws.cell(row=i+1, column=4).value = music

wb.save('Example.xlsx')