0

I need to write a data table in excel file. This data table has 7 rows and 1 column, and all the values are different. My problem is only last row is written in excel file.

What I need in excel:

FT 202003/69362
FT 202003/62581
FT 202003/41307
FT 202003/32316
FT 202003/4664
FT 201903/215090
FT 201903/197043

What is happening in excel:

FT 201903/197043
FT 201903/197043
FT 201903/197043
FT 201903/197043
FT 201903/197043
FT 201903/197043
FT 201903/197043

My code:

faturas = driver.find_elements_by_xpath("//p[@class='text-description-small']//b[contains(text(),'FT')]")
    totalfaturas = len(faturas)
    for i in range(totalfaturas):
        fat = faturas[i].text
        print(fat)

    from openpyxl import *

    nf = load_workbook("C:\WebDrivers\Fatturas.xlsx")
    nf1 = nf.active
    for n in range(1, totalfaturas+1):
            nf1.cell(row=n,column=1).value=fat
            nf.save("C:\WebDrivers\Fatturas.xlsx")

I can print the right data table in python, but I can't write this data table in excel. Can you please help me?

2
  • nf1.cell(row=n,column=1).value=fat => this is wrong . why using fat here ? just do same as you did in above loop , dynamically :) Commented May 28, 2020 at 11:58
  • Thanks for your answer! Can you be specific? I didn't undestand how should I use that... Commented May 28, 2020 at 12:07

1 Answer 1

1

Change this:

for i in range(totalfaturas):
    fat = faturas[i].text
    print(fat)

to:

fat_list=[]
for fat in faturas:
    fat_list.append(fat.text)
    #print(fat.text)

Then, change this:

for n in range(1, totalfaturas+1):
        nf1.cell(row=n,column=1).value=fat
        nf.save("C:\WebDrivers\Fatturas.xlsx")

to:

for item in fat_list:
    nf1.cell(row=fat_list.index(item)+1,column=1).value=item
    nf.save("C:\WebDrivers\Fatturas.xlsx")
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.