Question

How can I share information between a Python Script and a variable in a Excel Loop

  • 21 December 2022
  • 2 replies
  • 235 views

Userlevel 1
Badge +7
I created a Python script that gets the dollar value from an external website. I am not able to transfer this value to a variable to be stored in an excel spreadsheet. How can I do this: share information obtained through a Python script to be recorded in an Excel spreadsheet, using a variable in a Loop? Bellow is the Script Code. I created a Manual Script and ran it with the instruction "Python Script: Open". Note that the dollar value will be stored in the variable "valorDolarPeloGoogle", however I cannot put this variable in a "Message" instruction. How to do this?

#Importamos o selenium para trabalhar com as páginas da web
from selenium import webdriver as opcoes_selenium_aula
from selenium.webdriver.common.keys import Keys

#Importando a biblioteca do pyautogui para trabalhar com o tempo e teclas teclado
import pyautogui as tempoPausaComputador

#Usando o By para trabalhar com as atualizações mais recentes
from selenium.webdriver.common.by import By

#Passamos autorização ao acesso as configurações do Chrome
meuNavegador = opcoes_selenium_aula.Chrome()
meuNavegador.get("https://www.google.com.br/")

#Aguarda 4 segundo para dar tempo do computador processar as informações
tempoPausaComputador.sleep(4)

#Procurando pelo elemento NAME e quando encontrar vou escrever Dolar hoje
meuNavegador.find_element(By.NAME, "q").send_keys("Dolar hoje")

#Aguarda 4 segundo para dar tempo do computador processar as informações
tempoPausaComputador.sleep(4)

#Retorna para o campo name q
#Faz a busca do valor que está digitado no campo NAME q
meuNavegador.find_element(By.NAME, "q").send_keys(Keys.RETURN)

#Aguarda 4 segundo para dar tempo do computador processar as informações
tempoPausaComputador.sleep(4)

#No resultado da pesquisa pegamo o XPATH e no meios pegamos o primeiro elemento da lista
valorDolarPeloGoogle = meuNavegador.find_elements(By.XPATH, '//*[@id="knowledge-currency__updatable-data-column"]/div[1]/div[2]/span[1]')[0].text

#Aguarda 4 segundo para dar tempo do computador processar as informações
tempoPausaComputador.sleep(4)


 


2 replies

Userlevel 3
Badge +6

Dear @interplan,

 

To transfer the value stored in the valorDolarPeloGoogle variable to an Excel spreadsheet, you will need to use a library that allows you to read and write to Excel files. One such library is openpyxl. Here's an example of how you can use it to write the value of valorDolarPeloGoogle to a cell in an Excel spreadsheet:

import openpyxl

# Open the spreadsheet and get the active sheet
wb = openpyxl.load_workbook('spreadsheet.xlsx')
sheet = wb.active

# Write the value of valorDolarPeloGoogle to cell A1
sheet['A1'] = valorDolarPeloGoogle

# Save the changes to the spreadsheet
wb.save('spreadsheet.xlsx')
 

You can then use a "Message" instruction to display a confirmation message or any other information you want to convey to the user.

If you want to write the value of valorDolarPeloGoogle to the spreadsheet repeatedly in a loop, you can simply place the code above inside the loop. Make sure to save the changes to the spreadsheet after each iteration so that the new value gets written to the file.

 

Regards,

Userlevel 1
Badge +7

Excelent answer, Zaibi.

I Will try it soon.

Thanks a Lot

Reply