RPA.Excel.Application

module RPA.Excel.Application

class RPA.Excel.Application.Application

Application(*args, **kwargs)

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

Examples

Robot Framework

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.

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:


variable APP_DISPATCH

APP_DISPATCH = 'Excel.Application'

variable ROBOT_LIBRARY_DOC_FORMAT

ROBOT_LIBRARY_DOC_FORMAT = 'REST'

variable ROBOT_LIBRARY_SCOPE

ROBOT_LIBRARY_SCOPE = 'GLOBAL'

method add_new_sheet

add_new_sheet(sheetname: str, create_workbook: bool = True)

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

method add_new_workbook

add_new_workbook()

Adds new workbook for Excel application


property app

property app

method close_document

close_document(save_changes: bool = False)

Close the active document and app (if open).

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

method export_as_pdf

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

method find_first_available_cell

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

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

method find_first_available_row

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

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

method open_application

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

Open the application.

Parameters
  • visible – Show the window on opening. (False by default)
  • display_alerts – Display alert popups. (False by default)

method open_workbook

open_workbook(filename: str)

Open Excel by filename

By default sets active worksheet to sheet number 1

  • Parameters: filename – path to filename

method quit_application

quit_application(save_changes: bool = False)

Quit the application.

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

method read_from_cells

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

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

method run_macro

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

method save_excel

save_excel()

Saves Excel file


method save_excel_as

save_excel_as(filename: str, autofit: bool = False, file_format=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


method set_active_worksheet

set_active_worksheet(sheetname: str = None, sheetnumber: int = 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

method write_to_cells

write_to_cells(worksheet: Any = None, row: int = None, column: int = None, value: str = None, number_format: str = None, formula: str = 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