Python API

Excel.Application

class RPA.Word.Application.Application(autoexit: bool = True)

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

Examples

Robot Framework

*** Settings ***
Library                 RPA.Word.Application
Task Setup              Open Application
Suite Teardown          Quit Application

*** Tasks ***
Open existing file
    Open File           old.docx
    Write Text          Extra Line Text
    Write Text          Another Extra Line of Text
    Save Document AS    ${CURDIR}${/}new.docx
    ${texts}=           Get all Texts
    Close Document

Python

from RPA.Word.Application import Application

app = Application()
app.open_application()
app.open_file('old.docx')
app.write_text('Extra Line Text')
app.save_document_as('new.docx')
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 = 'Word.Application'
FILEFORMATS = {'DEFAULT': 'wdFormatDocumentDefault', 'HTML': 'wdFormatHTML', 'OPENDOCUMENT': 'wdFormatOpenDocumentText', 'PDF': 'wdFormatPDF', 'RTF': 'wdFormatRTF', 'WORD97': 'wdFormatDocument97'}
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
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)

copy_selection_to_clipboard()

Copy current text selection to clipboard.

create_new_document() None

Create new document for Word application

export_to_pdf(filename: str) None

Export active document into PDF file.

Parameters

filename – PDF to export WORD into

find_text(text: str, cursor_position: CursorPosition = CursorPosition.NO_MOVE, copy: bool = False) None

Find text in the document.

Parameters
  • text – text to find

  • cursor_position – where to move cursor after finding text

  • copy – copy found text into clipboard

Raises

AssertionError – if text is not found

get_all_texts() str

Get all texts from active document

Returns

texts

get_current_line() str

Get the text of the current line in the document.

get_number_of_lines() int

Get the number of lines in the document.

move_horizontally(characters: int = 0) Any

Move cursor horizontally from current cursor position.

Remember that if cursor is already at the start the cursor can’t move left and if cursor is already at the end the cursor can’t move right.

Parameters

characters – characters to move

move_to_end() None

Move cursor to the end of the document.

move_to_line_end() None

Move cursor to end of the line on the current cursor position.

move_to_line_start() None

Move cursor to start of the line on the current cursor position.

move_to_top() None

Move cursor to the top of the document.

move_vertically(lines: int = 0) Any

Move cursor vertically from current cursor position.

Remember that if cursor is already at the top the cursor can’t move up and if cursor is already at the bottom the cursor can’t move down.

Parameters

lines – lines to move

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_file(filename: str, read_only: bool = True) None

Open Word document with filename.

Parameters

filename – Word document path

paste_from_clipboard() None

Paste content from clipboard to the document’s current cursor position.

quit_application(save_changes: bool = False) None

Quit the application.

Parameters

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

replace_text(find: str, replace: str) None

Replace text in active document

Parameters
  • find – text to replace

  • replace – new text

save_document() None

Save active document

save_document_as(filename: str, fileformat: Optional[str] = None) None

Save document with filename and optionally with given fileformat

Parameters
  • filename – where to save document

  • fileformat – see @FILEFORMATS dictionary for possible format, defaults to None

select_current_paragraph()

Select text in current active paragraph.

select_paragraph(count: int = 1)

Select paragraph(s) from current cursor position.

Negative count moves cursor up number of paragraphs and positive count moves cursor down number of paragraphs.

Parameters

count – number of paragraphs to select

Set footer for the active document

Parameters

text – footer text to set

set_header(text: str) None

Set header for the active document

Parameters

text – header text to set

set_object_property(object_instance, property_name: str, value: str)

Set the property of any object.

This is a utility keyword for Robot Framework syntax to set object property values.

${new_value}=    Replace String    ${value}    10.132.    5511.11.
Set Object Property    ${result}    Value    ${new_value}
Parameters
  • object_instance – object instance to set the property

  • property_name – property name to set

  • value – value to set

write_text(text: str, cursor_position: CursorPosition = CursorPosition.NO_MOVE, end_of_text: bool = True) None

Writes given text at the end of the document

Parameters
  • text – string to write

  • cursor_position – where to move cursor before writing

  • end_of_text – if True moves cursor to the end of the text before writing