Morse
Morse

Reputation: 11

Openpyxl printing a list through column

I'm working with Selenium and Openpyxl in the current version of python.

Essentially I am scraping an online list and trying to import that list into an existing excel file, where I can then parse the data and work with it separately.

My current issue is being able to print this data into separate excel rows. I've only been using python for a couple of weeks, so I'm pretty new at this. I tried using the append function, but that didn't print anything. Using ws.cell(row=1,column=1,value=td), I can repeatedly print to the first cell, but I need to print each value to a different cell.

import openpyxl
from openpyxl import Workbook

wb = openpyxl.load_workbook('formulaFile.xlsx')
ws = wb.active

datapoint1 = driver.find_elements_by_tag_name("td")
wb = openpyxl.load_workbook('formulaFile.xlsx')
ws = wb.active

for td in allrows1:
    td=td.text
    print (td)
    #ws.append([td])
    ws.cell(row=1, column=1, value=ta)
wb.save('formulaTest.xlsx')

I believe my biggest issue is not knowing what to set that row value equal to, so that it dynamically changes the row for each new value.

Upvotes: 1

Views: 2367

Answers (1)

Ben.T
Ben.T

Reputation: 29635

one way to do it is to have a variable with your row number, let's call it line, incrementing at each loop in your for. You can do like:

line=1 #initialize your variable at 1
for td in allrows1:
    td=td.text
    print (td)
    ws.cell(row=line, column=1, value=ta) # change 1 with line
    line += 1 # add one to line for the next iteration

If the rest of your code works, then you will have all your data in different cells (rows)

Upvotes: 0

Related Questions