libre office

Karim kliateni at gmail.com
Wed Jan 20 18:03:33 EST 2016



On 20/01/2016 00:01, jim-pc wrote:
> How do I get data from libre office using python?
>

Hello,

The function below search and get text between 2 delimiters inside a 
libre office swriter document.
It is given as an example. You still have to open a document object in 
Swriter server.(In the Libre office UNO website you will find simple 
code to do that step.)

def get_text_inside_delimiters(component=None, delimiter1=None, 
delimiter2=None):
     """ Get text between 2 string delimiters."""

     # Creation of 2 descriptors from a document w/ search capabilities.
     open_search  = component.createSearchDescriptor()
     close_search = component.createSearchDescriptor()

     # Specify the delimiter texts to find.
     open_search.SearchString  = delimiter1
     close_search.SearchString = delimiter2

     # Find and open the first delimiter object.
     open_found = component.findFirst(open_search)
     # Unselect the 1rst delimiter from the range selection
     # done by findFirst() to not have in the search result.
     open_found.goRight(0, False)

     # Search for the 2nd delimiter the closest from first delimiter.
     close_found = component.findNext(open_found.End, close_search)
     # Filter the 2nd delimiter from the range selection
     # done by findNext() to not have in the search result.
     close_found.goLeft(len(close_found.getString()), False)

     # Cursor selection of the target string.
     open_found.gotoRange(close_found, True)

     # Return target string whithin the cursor selection.
     return open_found.getString().strip()

Hope this help.
Karim




More information about the Python-list mailing list