Python API

Excel.Application

class RPA.Excel.Application.Application(*args, **kwargs)

Excel.Application is a library for controlling the Excel application.

Examples

Robot Framework

*** Settings ***
Library             RPA.Excel.Application
Task Setup          Open Application
Task Teardown       Quit Application

*** Tasks ***
Manipulate Excel application
    Open Workbook           workbook.xlsx
    Set Active Worksheet    sheetname=new stuff
    Write To Cells          row=1
    ...                     column=1
    ...                     value=my data
    Save Excel

Run Excel Macro
    Open Workbook   orders_with_macro.xlsm
    Run Macro       Sheet1.CommandButton1_Click

Export Workbook as PDF
    Open Workbook           workbook.xlsx
    Export as PDF           workbook.pdf

Python

from RPA.Excel.Application import Application

app = Application()

app.open_application()
app.open_workbook('workbook.xlsx')
app.set_active_worksheet(sheetname='new stuff')
app.write_to_cells(row=1, column=1, value='new data')
app.save_excel()
app.quit_application()

Caveats

This library works on a Windows operating system with UI enabled only, and you must ensure that you open the app first with Open Application before running any other relevant keyword which requires to operate on an open app. The application is automatically closed at the end of the task execution, so this can be changed by importing the library with the autoexit=${False} setting.

*** Settings ***
Library     RPA.Excel|Outlook|Word.Application    autoexit=${False}

If you’re running the Process by Control Room through a custom self-hosted Worker service, then please make sure that you enable an RDP session by ticking “Use Desktop Connection” under the Step configuration.

If you still encounter issues with opening a document, please ensure that file can be opened first manually and dismiss any alert potentially blocking the process.

Check the documentation below for more info:

APP_DISPATCH = 'Excel.Application'
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
add_new_sheet(sheetname: str, create_workbook: bool = True) None

Add new worksheet to workbook. Workbook is created by default if it does not exist.

Parameters
  • sheetname – name for sheet

  • create_workbook – create workbook if True, defaults to True

Raises

ValueError – error is raised if workbook does not exist and create_workbook is False

add_new_workbook() None

Adds new workbook for Excel application

property app
close_document(save_changes: bool = False) None

Close the active document and app (if open).

Parameters

save_changes – Enable changes saving on quit. (False by default)

export_as_pdf(pdf_filename: str, excel_filename: Optional[str] = None)

Export Excel as PDF file

If Excel filename is not given, the currently open workbook will be exported as PDF.

Parameters
  • pdf_filename – PDF filename to save

  • excel_filename – Excel filename to open

find_first_available_cell(worksheet: Any = None, row: int = 1, column: int = 1) Any

Find first available free cell

Parameters
  • worksheet – worksheet to handle, defaults to active worksheet if None

  • row – starting row for search, defaults to 1

  • column – starting column for search, defaults to 1

Returns

tuple (row, column) or (None, None) if not found

find_first_available_row(worksheet: Optional[Any] = None, row: int = 1, column: int = 1) Any

Find first available free row

Parameters
  • worksheet – worksheet to handle, defaults to active worksheet if None

  • row – starting row for search, defaults to 1

  • column – starting column for search, defaults to 1

Returns

row or None

open_application(visible: bool = False, display_alerts: bool = False) None

Open the application.

Parameters
  • visible – Show the window on opening. (False by default)

  • display_alerts – Display alert popups. (False by default)

open_workbook(filename: str) None

Open Excel by filename

By default sets active worksheet to sheet number 1

Parameters

filename – path to filename

quit_application(save_changes: bool = False) None

Quit the application.

Parameters

save_changes – Enable to save changes on quit. (False by default)

read_from_cells(worksheet: Any = None, row: int = None, column: int = None) str

Read value from cell.

Parameters
  • worksheet – worksheet to handle, defaults to active worksheet if None

  • row – target row, defaults to None

  • column – target row, defaults to None

Raises

ValueError – if cell is not given

run_macro(macro_name: str, *args: Any)

Run Excel macro with given name

Parameters
  • macro_name – macro to run

  • args – arguments to pass to macro

save_excel() None

Saves Excel file

save_excel_as(filename: str, autofit: bool = False, file_format=None) None

Save Excel with name if workbook is open

Parameters
  • filename – where to save file

  • autofit – autofit cell widths if True, defaults to False

  • file_format – format of file

Note: Changing the file extension for the path does not affect the actual format. To use an older format, use the file_format argument with one of the following values:

https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat

Examples:

# Save workbook in modern format
Save excel as    orders.xlsx

# Save workbook in Excel 97 format (format from above URL)
Save excel as    legacy.xls   file_format=${56}
set_active_worksheet(sheetname: str = None, sheetnumber: int = None) None

Set active worksheet by either its sheet number or name

Parameters
  • sheetname – name of Excel sheet, defaults to None

  • sheetnumber – index of Excel sheet, defaults to None

write_to_cells(worksheet: Any = None, row: int = None, column: int = None, value: str = None, number_format: str = None, formula: str = None) None

Write value, number_format and/or formula into cell.

Parameters
  • worksheet – worksheet to handle, defaults to active worksheet if None

  • row – target row, defaults to None

  • column – target row, defaults to None

  • value – possible value to set, defaults to None

  • number_format – possible number format to set, defaults to None

  • formula – possible format to set, defaults to None

Raises

ValueError – if cell is not given