Class Selection
- class ooodev.utils.selection.Selection[source]
Selection Framework
- property text_range_compare
Static read-only property
Gets text range for comparison operations
- Returns:
Text Range Compare instance
- Return type:
XTextRangeCompare
- classmethod compare_cursor_ends(c1, c2)[source]
Compares two cursors ranges end positions.
Method NOT is Lo Instance Safe. Use Class LoContext when using with multiple document instances.
- Parameters:
c1 (XTextRange) – first cursor range
c2 (XTextRange) – second cursor range
- Raises:
Exception – if comparison fails
- Returns:
Compare result.
CompareEnum.BEFORE
ifc1
end position is beforec2
end position.CompareEnum.EQUAL
ifc1
end position is equal toc2
end position.CompareEnum.AFTER
ifc1
end position is afterc2
end position.- Return type:
- static get_cursor() com.sun.star.text.XTextCursor [source]
- static get_cursor(cursor_obj: Any) com.sun.star.text.XTextCursor
- static get_cursor(rng: com.sun.star.text.XTextRange, txt: com.sun.star.text.XText) com.sun.star.text.XTextCursor
- static get_cursor(rng: com.sun.star.text.XTextRange, text_doc: com.sun.star.text.XTextDocument) com.sun.star.text.XTextCursor
- static get_cursor(*args, **kwargs)
Gets text cursor.
- Parameters:
cursor_obj (DocOrCursor) – Text Document or Text View Cursor.
rng (XTextRange) – Text Range Instance.
text_doc (XTextDocument) – Text Document instance.
- Raises:
CursorError – If Unable to get cursor.
- Returns:
Cursor or
None
if unable to get cursor.- Return type:
XTextCursor | None
- classmethod get_left_cursor(o_sel, o_text)[source]
Creates a new TextCursor with position left that can travel right.
Method NOT is Lo Instance Safe. Use Class LoContext when using with multiple document instances.
- Parameters:
o_sel (XTextRange) – Text Range.
o_text (DocOrText) – Text document or text.
- Returns:
a new instance of a TextCursor which is located at the specified TextRange to travel in the given text context.
- Return type:
XTextCursor
- classmethod get_paragraph_cursor(cursor_obj)[source]
Gets document paragraph cursor.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
cursor_obj (DocOrCursor) – Text Document or Text Cursor.
- Raises:
ParagraphCursorError – If Unable to get cursor.
- Returns:
Paragraph cursor.
- Return type:
XParagraphCursor
- classmethod get_position(cursor)[source]
Gets position of the cursor.
Method NOT is Lo Instance Safe. Use Class LoContext when using with multiple document instances.
- Parameters:
cursor (XTextCursor) – Text Cursor.
- Returns:
Current Cursor Position.
- Return type:
int
Note
This method is not the most reliable. It attempts to read all the text in a document and move the cursor to the end and then get the position.
It would be better to use cursors from relative positions in bigger documents.
- classmethod get_right_cursor(o_sel, o_text)[source]
Creates a new TextCursor with position right that can travel left.
Method NOT is Lo Instance Safe. Use Class LoContext when using with multiple document instances.
- Parameters:
o_sel (XTextRange) – Text Range
o_text (DocOrText) – Text document or text.
- Returns:
a new instance of a TextCursor which is located at the specified TextRange to travel in the given text context.
- Return type:
XTextCursor
- static get_selected_text_range(text_doc)[source]
Gets the text range for current selection.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document.
- Raises:
MissingInterfaceError – If unable to obtain required interface.
- Returns:
If no selection is made then None is returned; Otherwise, Text Range.
- Return type:
XTextRange | None
Note
Writer must be visible for this method or
None
is returned.
- classmethod get_selected_text_str(text_doc)[source]
Gets the first selection text for Document.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document.
- Returns:
Selected text or empty string.
- Return type:
str
Note
Writer must be visible for this method or empty string is returned.
- classmethod get_sentence_cursor(cursor_obj)[source]
Gets document sentence cursor.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
cursor_obj (DocOrCursor) – Text Document or Text Cursor.
- Raises:
SentenceCursorError – If Unable to get cursor.
- Returns:
Sentence Cursor.
- Return type:
XSentenceCursor
Changed in version 0.16.0: Now raises
SentenceCursorError
instead of returningNone
.
- classmethod get_text_cursor_props(text_doc)[source]
Gets properties for document cursor.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document
- Raises:
MissingInterfaceError – If unable to obtain XPropertySet interface from cursor.
- Returns:
Properties
- Return type:
XPropertySet
- classmethod get_text_doc() com.sun.star.text.XTextDocument [source]
- classmethod get_text_doc(doc: com.sun.star.lang.XComponent) com.sun.star.text.XTextDocument
- classmethod get_text_doc(doc=None)
Gets a writer document
When using this method in a macro the
Lo.get_document()
value should be passed asdoc
arg.- Parameters:
doc (XComponent) – Component to get writer document from
- Raises:
TypeError – doc is None
MissingInterfaceError – If doc does not implement XTextDocument interface
- Returns:
Writer document
- Return type:
XTextDocument
- Events:
Changed in version 0.9.0: Added overload
get_text_doc()
- classmethod get_text_view_cursor_prop_set(text_doc)[source]
Gets properties for document view cursor.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document.
- Raises:
MissingInterfaceError – If unable to obtain XPropertySet interface from cursor.
- Returns:
Properties.
- Return type:
XPropertySet
- static get_view_cursor(text_doc)[source]
Gets document view cursor.
Describes a cursor in a text document’s view.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document
- Raises:
ViewCursorError – If Unable to get cursor
- Returns:
Text View Cursor
- Return type:
XTextViewCursor
See also
- static get_word_count_ooo(text, word_type=None, locale_lang=None)[source]
Get the number of word in ooo way.
This method takes into account the current Locale.
Method NOT is Lo Instance Safe. Use Class LoContext when using with multiple document instances.
- Parameters:
text (str) – string to count the word of.
word_type (WordTypeEnum, optional) – type of words to count. Default
WordTypeEnum.WORD_COUNT
Import linefrom ooodev.utils.selection import WordTypeEnum
.locale_lang (str, optional) – Language such as ‘en-US’ used to process word boundaries. Defaults to LO’s current language.
- Raises:
CreateInstanceMsfError – If unable to create
i18n.BreakIterator service
.- Returns:
The number of words.
- Return type:
int
- classmethod get_word_cursor(cursor_obj)[source]
Gets document word cursor.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
cursor_obj (DocOrCursor) – Text Document or Text Cursor.
- Raises:
WordCursorError – If Unable to get cursor.
- Returns:
Word Cursor.
- Return type:
XWordCursor
- static is_anything_selected(text_doc)[source]
Determine if anything is selected.
If Write document is not visible this method returns false.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document
- Returns:
True if anything in the document is selected: Otherwise, False
- Return type:
bool
Note
Writer must be visible for this method or
False
is always returned.
- classmethod range_len(text_doc, o_sel)[source]
Gets the distance between range start and range end.
Method NOT is Lo Instance Safe. Use Class LoContext when using with multiple document instances.
- Parameters:
o_sel (XTextCursor) – first cursor range
o_text (object) – XText object, usually document text object
text_doc (XTextDocument) –
- Returns:
length of range
- Return type:
int
Note
All characters are counted including paragraph breaks. In Writer it will display selected characters however, paragraph breaks are not counted.
- classmethod select_next_word(text_doc)[source]
Select the word right from the current cursor position.
Method is Lo Instance Safe for use with multiple documents.
- Parameters:
text_doc (XTextDocument) – Text Document
- Returns:
True if go to next word succeeds; Otherwise, False.
- Return type:
bool
- Events:
Note
The method returning
True
does not necessarily mean that the cursor is located at the next word, or any word at all! This may happen for example if it travels over empty paragraphs.Note
Event args
event_data
is a dictionary containingtext_doc
.