Search code examples
pythonopenoffice-writerpyuno

How to get Text coordinates using PyUNO with OpenOffice writer


I have a python script that successfully does a search and replace in an OpenOffice Writer document using PyUNO. I wanna to ask how to get the coordinate of found text?

import string
search = document.createSearchDescriptor()
search.SearchString = unicode('find')
#search.SearchCaseSensitive = True
#search.SearchWords = True
found = document.findFirst(search)
if found:
    #log.debug('Found %s' % find)
    ## any code here help to get the coordinate of found text?
    pass

Solution

  • This is some StarBASIC code to find the page number of a search expression in a Writer document:

    SUB find_page_number()
    
    oDoc = ThisComponent
    oViewCursor = oDoc.getCurrentController().getViewCursor()
    oSearchFor = "My Search example"
    
    oSearch = oDoc.createSearchDescriptor()
    With oSearch
       .SearchRegularExpression = False
       .SearchBackwards = False
       .setSearchString(oSearchFor)
    End With
    
    oFirstFind = oDoc.findFirst(oSearch)
    
    
    If NOT isNull(oFirstFind) Then
        oViewCursor.gotoRange(oFirstFind, False)
        MsgBox oViewCursor.getPage()
    Else
       msgbox "not found: " & oSearchFor
    End If
    

    Hope this helps you