Using createsearchdescriptor in Python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
karpo518
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Using createsearchdescriptor in Python

Post by karpo518 »

Hi. I have code of searchdescriptor cycle, but i'dont know, how to stop it correctly.

Code: Select all

oFound = oDoc.findFirst(oSearch)
while oFound is not None:
        oFound = oDoc.findNext(oFound.End, oSearch)
Condition 'oFound is not None' does not works. I used code 'do while not IsNull(oFound)' in basic, but i have trouble in python. How to do it?
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Using createsearchdescriptor in Python

Post by Villeroy »

Code: Select all

while oFound:
loops while oFound is none of None, 0, False, empty string, empty dictionary, empty list, empty tuple.

Are you sure you want to program complex things in Python without knowing the language?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
karpo518
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: Using createsearchdescriptor in Python

Post by karpo518 »

Thanks for answer. ''while oFound:' does not works. When i does not get result, loop does not breaks. I have error:

Code: Select all

com.sun.star.uno.RuntimeException (Error during invoking function PlainToHTML2 in module file:///usr/lib/libreoffice/share/Scripts/python/PlainToText2.py (<class 'uno.com.sun.star.lang.IllegalArgumentException'>: 
  /usr/lib/libreoffice/share/Scripts/python/PlainToText2.py:98 in function FormatWorker() [if oDoc.Text.compareRegionEnds(oFound, oRCurs)>=0:]
  /usr/lib/libreoffice/share/Scripts/python/PlainToText2.py:43 in function PlainToHTML2() [FormatWorker(oCurs[i][0], oCurs[i][1], model, ".*")]
  /usr/lib/libreoffice/program/pythonscript.py:870 in function invoke() [ret = self.func( *args )]
))
It happens, becouse oFound does not contains result. How can I solve this problem?
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: Using createsearchdescriptor in Python

Post by karpo518 »

I need somethink as

Code: Select all

if not oFound.supportsService("search result"):
    break
becouse type of oFound is 'PyUNO' always
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Using createsearchdescriptor in Python

Post by hubert lambert »

Hi,

Try this:

Code: Select all

oFound = oDoc.findAll(oSearch)
for elem in oFound:
        # do your stuff with elem
or this:

Code: Select all

elem = oDoc.findFirst(oSearch)
while elem:
    # do your stuff with elem
    elem = doc.findNext(elem, oSearch)
Both work for me in LibreOffice.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
karpo518
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: Using createsearchdescriptor in Python

Post by karpo518 »

I undestanded. It is other error. I use compareRegionEnds as condition for break in search results processing loop. I need it for search in selected text only. If oFound is table data, i have error in compareRegionEnds function.
How i can solve this problem? I need detect paragraph data in loop. This issue was discussed here viewtopic.php?f=45&t=6756 but no solutions there
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Using createsearchdescriptor in Python

Post by hubert lambert »

Table cells have their own text object, independent from the document main text object. Therrefore you can't compare their starts and ends.
If the found string is inside a table cell, the property "TextTable" points to the table, which have an anchor attached to the main text (if not in an other table or frame of course). Then you can compare start and end of the table anchor with the ones of your selection.
Hope this can help...
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
karpo518
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: Using createsearchdescriptor in Python

Post by karpo518 »

It helps me to detect table data. What is about Frames and other data types? I need universal solution for every cases. I need detect simple text data only.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Using createsearchdescriptor in Python

Post by hubert lambert »

You can check for an in-frame text with the property TextFrame. Text within shapes, as far as I know, are not considered by the methods "findXXX".
I don't see right now other object that might embed his own text objects.

Much simpler by the way, you may try comparing text objects :

Code: Select all

    if oFound.Text == oDoc.Text:
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Post Reply