Downloading and reading an Excel file

This example demonstrates how to download and how to read data from an Excel file. We will use RPA.Excel.Files and RPA.HTTP libraries from the RPA Framework.

Robot Framework example

*** Settings ***
Documentation       Download an Excel file and read the rows.

Library             RPA.Excel.Files
Library             RPA.HTTP


*** Variables ***
${EXCEL_FILE_URL}=      https://github.com/robocorp/example-web-store-order-processor/raw/main/devdata/Data.xlsx


*** Tasks ***
Download an Excel file and read the rows
    Download    ${EXCEL_FILE_URL}    overwrite=True
    Open Workbook    Data.xlsx
    ${table}=    Read Worksheet As Table    header=True
    Close Workbook

    FOR    ${row}    IN    @{table}
        Log    ${row}
    END

Python example

from RPA.Excel.Files import Files
from RPA.HTTP import HTTP

excel_lib = Files()
http_lib = HTTP()

excel_file_url= "https://github.com/robocorp/example-web-store-order-processor/raw/main/devdata/Data.xlsx"


def download_the_excel_file(url):
    http_lib.download(url, overwrite=True)

def open_the_file_as_table():
    excel_lib.open_workbook("Data.xlsx")
    table = excel_lib.read_worksheet_as_table(header=True)
    excel_lib.close_workbook()
    return table

def loop_over_table(table):
    for row in table:
        print(row)

# Define a main() function that calls the other functions in order:
def main():
    download_the_excel_file(excel_file_url)
    table = open_the_file_as_table()
    loop_over_table(table)

# Call the main() function, checking that we are running as a stand-alone script:
if __name__ == "__main__":
    main()

See RPA.Excel.Files keyword documentation to learn more about working with Excel files.

May 5, 2022