Skip to content

Instantly share code, notes, and snippets.

@mikahanninen
Last active February 20, 2024 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikahanninen/24e4d948c592841064ed89d02f372f93 to your computer and use it in GitHub Desktop.
Save mikahanninen/24e4d948c592841064ed89d02f372f93 to your computer and use it in GitHub Desktop.
from RPA.Word.Application import Application
from win32com.client import constants
from typing import Any
from enum import Enum
class CursorPosition(Enum):
NO_MOVE = 0
START = 1
END = 2
class DevWord(Application):
def __init__(self, autoexit: bool = True) -> None:
super().__init__(autoexit)
def paste_from_clipboard(self) -> None:
"""Paste content from clipboard to the document's
current cursor position."""
self.app.Selection.Paste()
def get_current_line(self) -> str:
"""Get the text of the current line in the document."""
original_range = self.app.Selection.Range
self.app.Selection.Expand(Unit=constants.wdLine)
text = self.app.Selection.Text
original_range.Select()
return text
def get_number_of_lines(self) -> int:
"""Get the number of lines in the document."""
if self.app.Documents.Count >= 1:
return self.app.Documents.Item(1).ComputeStatistics(
constants.wdStatisticLines
)
else:
raise AssertionError("No document is open")
def move_to_top(self) -> None:
"""Move cursor to the top of the document."""
self.app.Selection.HomeKey(Unit=constants.wdStory)
def move_to_end(self) -> None:
"""Move cursor to the end of the document."""
self.app.Selection.EndKey(Unit=constants.wdStory)
def move_to_line_start(self, selection: Any = None) -> None:
"""Move cursor to start of the line on the current cursor position."""
self.app.Selection.HomeKey(Unit=constants.wdLine)
def move_to_line_end(self, selection: Any = None) -> None:
"""Move cursor to end of the line on the current cursor position."""
self.app.Selection.EndKey(Unit=constants.wdLine)
def move_vertically(
self,
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.
:param lines: lines to move
"""
if lines > 0:
self.app.Selection.MoveDown(Unit=constants.wdLine, Count=lines)
elif lines < 0:
self.app.Selection.MoveUp(Unit=constants.wdLine, Count=-lines)
self.app.Selection.Select()
def move_horizontally(
self,
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.
:param characters: characters to move
"""
if characters > 0:
self.app.Selection.MoveRight(Unit=constants.wdCharacter, Count=characters)
elif characters < 0:
self.app.Selection.MoveLeft(Unit=constants.wdCharacter, Count=-characters)
self.app.Selection.Select()
def find_text(
self,
text: str,
cursor_position: CursorPosition = CursorPosition.NO_MOVE,
copy: bool = False,
) -> None:
"""Find text in the document.
:param text: text to find
:param copy: copy found text into clipboard
:raises AssertionError: if text is not found
"""
found = False
content_range = None
if self.app.Documents.Count >= 1:
content_range = self.app.Documents.Item(1).Content
content_range.Find.ClearFormatting()
found = content_range.Find.Execute(FindText=text)
else:
raise AssertionError("No document is open")
if not found:
raise AssertionError(f"Text '{text}' not found in the document")
if copy:
content_range.Copy()
if cursor_position != CursorPosition.NO_MOVE:
direction = (
constants.wdCollapseStart
if cursor_position == CursorPosition.START
else constants.wdCollapseEnd
)
content_range.Collapse(Direction=direction)
def write_text(
self,
text: str,
cursor_position: CursorPosition = CursorPosition.NO_MOVE,
end_of_text: bool = True,
) -> None:
"""Writes given text at the end of the document
:param text: string to write
:param overwrite: overwrite text if True otherwise will insert text
:param end_of_text: if True moves cursor to the end of the text
before writing
"""
if end_of_text:
self.move_to_end()
if cursor_position != CursorPosition.NO_MOVE:
direction = (
constants.wdCollapseStart
if cursor_position == CursorPosition.START
else constants.wdCollapseEnd
)
self.app.Selection.Collapse(Direction=direction)
self.app.Selection.TypeText(text)
self.app.Selection.Select()
*** Settings ***
Library DevWord WITH NAME Word
Library RPA.Desktop
*** Tasks ***
DevWord keywords in use
Set Clipboard Value Added text to the clipboard
Word.Open Application # visible=True
Create New Document
Write Text mm/dd/yyyy\n\nRobocorp Control Room\n\n
Word.Paste from Clipboard
Write Text \n\n
# Create test content for the document
FOR ${i} IN RANGE 1 100
Write Text Line: ${i}\n
END
Move To Line Start
Move Vertically -4
Move Horizontally 2
Write Text ZZ end_of_text=False
Move To Line Start
Move Vertically 3
Move Horizontally 1
Write Text XX end_of_text=False
Replace Text mm/dd/yyyy 02/20/2024
Save Document As result.docx
Move To Top
${lines}= Get Number Of Lines
FOR ${i} IN RANGE 1 ${{$lines+1}}
${text}= Get Current Line
Move Vertically 1
Log To Console On line ${i} the text is: ${text}
END
Find Text Robocorp Control Room copy=True # move_cursor=end
Write Text AFTER FIND end_of_text=False cursor_position=end
Save Document
[Teardown] Word.Quit Application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment