1

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
Randy
  • 13
  • 3
  • What do you mean by "coordinates"? Position of text in a document depends on page/char sizes and other facts (screen/print resoltuion). What would you like to do with such values? – ngulam Jul 17 '14 at 12:17
  • purpose question is that I want to add a 'specified' text in odt file, and detect this text's postion for page size adjusting. – Randy Jul 17 '14 at 12:25

1 Answers1

1

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

ngulam
  • 995
  • 1
  • 6
  • 11