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

I am trying to insert values from a list into excel, I know that I can use a dictionary and will do the same, but I would like to do it this way from a list. The code appends the value but appends only one value. For instance, in the column appears the value of Salsa. Thank you in advance!

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")


Solution 1:[1]

Check the openpyxl docs; they include some good basic tutorials that will help you, especially for iterating over ranges of cells. iter_rows and iter_cols are also very useful tools that may help you here. A simple solution would consist of:

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')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1