Ask Your Question
0

How to search font styles using python

asked 2019-02-10 12:18:13 +0200

shemgp gravatar image

I would like to search for a characters/words with a given font style and set it's paragraph style using python macro. But I'm stuck with:

Unknown property: CharPosture (Error during invoking function search in module file:///home/shemgp/.config/libreoffice/4/user/Scripts/python/StylizeDocument.py (<class 'uno.com.sun.star.beans.UnknownPropertyException'>: Unknown property: CharPosture

File "/usr/lib/libreoffice/program/pythonscript.py", line 905, in invoke ret = self.func( *args ) File "/home/shemgp/.config/libreoffice/4/user/Scripts/python/StylizeDocument.py", line 18, in search search.setPropertyValue('CharPosture', FontSlant.ITALIC) ))

Here's my initial code:

def search():
    class FontSlant():
        from com.sun.star.awt.FontSlant import (NONE, ITALIC,)
    document = XSCRIPTCONTEXT.getDocument()
    search = document.createSearchDescriptor()
    search.SearchString = "(.*)"
    search.SearchAll = True
    search.SearchWords = True
    search.SearchRegularExpression = True
    search.SearchCaseSensitive = False
    search.setPropertyValue('CharPosture', FontSlant.ITALIC)
    selsFound = document.findAll(search)
    if selsFound.getCount() == 0:
                return
    for selIndex in range(0, selsFound.getCount()):
                selFound = selsFound.getByIndex(selIndex)
                selFound.setString("abc")
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2019-03-01 14:49:50 +0200

jmz gravatar image

updated 2019-03-01 14:50:51 +0200

Hi,

You have to provide the search attributes of the descriptor :

from com.sun.star.awt.FontSlant import ITALIC
from com.sun.star.beans import PropertyValue  
def shemgp():
    doc = XSCRIPTCONTEXT.getDocument()
    search = doc.createSearchDescriptor()
    search.SearchString = "(.*)"
    search.SearchAll = True
    search.SearchWords = True
    search.SearchRegularExpression = True
    search.SearchCaseSensitive = False
    prop = PropertyValue('CharPosture', 0, ITALIC, 0)
    search.SearchAttributes = (prop,)
    search.ReplaceString = "abc"
    found = doc.replaceAll(search)

Regards.

edit flag offensive delete link more

Comments

It worked. Thanks.

shemgp gravatar imageshemgp ( 2019-03-02 14:25:03 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-02-10 12:18:13 +0200

Seen: 83 times

Last updated: Mar 01