Downloading the Excel file

Each week, the actual sales data is exported from the company's sales system and uploaded to the intranet. Our robot will need to get the data out of the Excel file and fill the sales form for each sales representative. But first, it needs to download the Excel file.

As always, we add one more line (a function call) to our task. This time we will not add it at the end of the task, but before the fill_and_submit_form() function call:

from robocorp.tasks import task from robocorp import browser @task def robot_spare_bin_python(): """Insert the sales data for the week and export it as a PDF""" browser.configure( slowmo=100, ) open_the_intranet_website() log_in() download_excel_file() fill_and_submit_sales_form()

Then, as you probably guessed by now, we add a new function at the end of the file:

def download_excel_file(): """Downloads excel file from the given URL"""

Great! Now we have to implement the function.

Importing the robocorp.http library

Currently, our robot does not know how to download files from remote web servers. But by now, we know how to teach an old robot new skills: by importing a library!

Our trusty robocorp set of open-source python automation libraries includes the http module that adds the download function that allows us to, guess what, download a file, and that's just what we need.

We add a new module import line to our top section like this:

from robocorp.tasks import task from robocorp import browser, http

The download() function requires a URL as an argument. We know that we can expect our Excel file at https://robotsparebinindustries.com/SalesData.xlsx each week.

In addition to the URL argument, we are also setting the overwrite argument to True. This way, we can count on the local file being always the most recent version (if the file exists, the robot has our permission to overwrite it).

Our keyword becomes:

def download_excel_file(): """Downloads excel file from the given URL""" http.download(url="https://robotsparebinindustries.com/SalesData.xlsx", overwrite=True)

With these additions, our robot looks like this:

from robocorp.tasks import task from robocorp import browser, http @task def robot_spare_bin_python(): """Insert the sales data for the week and export it as a PDF""" browser.configure( slowmo=100, ) open_the_intranet_website() log_in() download_excel_file() fill_and_submit_sales_form() def open_the_intranet_website(): """Navigates to the given URL""" browser.goto("https://robotsparebinindustries.com/") def log_in(): """Fills in the login form and clicks the 'Log in' button""" page = browser.page() page.fill("#username", "maria") page.fill("#password", "thoushallnotpass") page.click("button:text('Log in')") def fill_and_submit_sales_form(): """Fills in the sales data and click the 'Submit' button""" page = browser.page() page.fill("#firstname", "John") page.fill("#lastname", "Smith") page.fill("#salesresult", "123") page.select_option("#salestarget", "10000") page.click("text=Submit") def download_excel_file(): """Downloads excel file from the given URL""" http.download(url="https://robotsparebinindustries.com/SalesData.xlsx", overwrite=True)

Let's run our robot now (Shift-Command-P (macOS) or Ctrl+Shift+P (Windows)).

Great! Our robot has downloaded the remote file (SalesData.xlsx). You should find the file amongst your other robot files. You could instruct the robot to download to a different location, but we are okay with the current working directory.

What we learned

  • You can use the download function to easily download remote files to your local computer.
  • The download function can be configured to overwrite an existing file of the same name if found.