1

I am trying to write multiple rows to Excel. "Containers" has an index/len of 10. Although I loop through it, I only get one row when I run the script.

Can anyone help me with this?

# define first empty row
start_row = sheet.max_row + 1

#write to excel
for container in containers:
 #Company name
 company_container = container.div.h2
 company = company_container.text.strip()

 #City
 city = container.div.a["title"]

 #URL
 url = container.div.div.a["href"]

 #Title
 title_container = container.findAll("div", {"class":"info"})
 title = title_container[0].img["alt"]

 #Description
 description = title_container[0].p.text

 #Programming language
 hardskill = "Java"

 #Vervolgactie (Teamleader)
 vervolgactie = "Nieuw"

 companyinfo = [company, city, url, title, description, hardskill, vervolgactie]

 for i in range(0,len(companyinfo)):
   e=sheet.cell(row=start_row,column=1+i)
   e.value=companyinfo[i]

wb.save("vacatures.xlsx")

1 Answer 1

1

Your problem is that you're writing everything out with e=sheet.cell(row=start_row,column=1+i) on the same row.

since everything in enclosed in the loop for container in containers, i'd just increment start_rowafter the sub loop.

 for i in range(0,len(companyinfo)):
   e=sheet.cell(row=start_row,column=1+i)
   e.value=companyinfo[i]

 start_row +=1 #not part of the above loop but part of the overarching loop

Another route i'd consider is to rename start_row to just row (this is more semantically clear) and have row defined in the loop

#start_row = sheet.max_row + 1

#write to excel
for container in containers:
 #row for this data to go on. 
 row = sheet.max_row + 1

 #Company name
 company_container = container.div.h2
 company = company_container.text.strip()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.