Python API¶
Excel.Application¶
-
class
RPA.Excel.Application.
Application
(autoexit: bool = True)¶ Bases:
object
Excel.Application is a library for controlling an 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()
-
ROBOT_LIBRARY_DOC_FORMAT
= 'REST'¶
-
ROBOT_LIBRARY_SCOPE
= 'GLOBAL'¶
-
add_new_sheet
(sheetname: str, tabname: str = None, 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
tabname – name for tab (deprecated)
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
-
close_document
(save_changes: bool = False) → None¶ Close the active document (if open).
-
export_as_pdf
(pdf_filename: str, excel_filename: 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: 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 Excel application.
- Parameters
visible – show window after opening
display_alerts – show alert popups
-
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.
-
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
-
-
RPA.Excel.Application.
catch_com_error
()¶ Try to convert COM errors to human readable format.