[Solved] Python API to replace text with bullet list

Keyboard macros or custom scripts

[Solved] Python API to replace text with bullet list

Postby mtran » Thu May 23, 2019 11:16 pm

I am able to use Python API to search and replace a placeholder text '_myPlaceholder_' in a .odt file.
I now want to replace this placeholder with a bulleted list (i.e. "Solid small circular bullets") with a list read in from a regular text file stored as a python list variable myList.
Here's the code snippet for the search and replace part:
Code: Select all   Expand viewCollapse view
import uno

# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()

# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
    "com.sun.star.bridge.UnoUrlResolver", localContext )

# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )

# get the central desktop object
desktop = ctx.ServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
document = desktop.loadComponentFromURL(ODT_FILE ,"_blank", 0, ())

# search and replace
search = document.createSearchDescriptor()
search.SearchString = '_myPlaceholder_'
found = document.findFirst(search)
found.String = found.String.replace('_myPlaceholder_', myList)

So basically, replace my placeholder with a multi line list prepended with the bullet character. I greatly appreciate any help or suggestions!
Last edited by Hagar Delest on Tue May 28, 2019 8:08 am, edited 1 time in total.
Reason: tagged solved
LibreOffice 6.1.5.2 on Windows 10
mtran
 
Posts: 3
Joined: Thu May 23, 2019 9:27 pm

Re: Python API to replace text with bullet list

Postby hubert lambert » Sun May 26, 2019 11:49 am

Hi,

If your place holder is a single paragraph, you could try something like this :
Code: Select all   Expand viewCollapse view
    [...]
    found = doc.findFirst(search)
    found.String = '\r'.join(myList)
    found.NumberingStyleName = "List 1"

If the place holder is "in line", part of a paragraph text, it is a little bit different has you have to add two paragraph breaks :
Code: Select all   Expand viewCollapse view
    [...]
    found = doc.findFirst(search)
    T = doc.Text
    T
.insertControlCharacter(found.Start, 0, False)
    found.String = '\r'.join(myList)
    currentnumstyle = found.NumberingStyleName
    found
.NumberingStyleName = "List 1"
    T.insertControlCharacter(found.End, 0, True)
    found.End.NumberingStyleName = currentnumstyle

Regards.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 126
Joined: Mon Jun 13, 2016 10:50 am

Re: Python API to replace text with bullet list

Postby mtran » Sun May 26, 2019 8:39 pm

Thanks Hubert for providing both cases for separate paragraph and inline replacements!
Also, I was only using \n, as I didn't know about the requirements for \r.

Just curious, would you know where I can find the various NumberingStyleName types?
LibreOffice 6.1.5.2 on Windows 10
mtran
 
Posts: 3
Joined: Thu May 23, 2019 9:27 pm

Re: Python API to replace text with bullet list

Postby hubert lambert » Mon May 27, 2019 8:22 pm

Numbering style names are just the one visible in the gui (F11). You can define any new personal style next to the predefined names.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 126
Joined: Mon Jun 13, 2016 10:50 am

Re: Python API to replace text with bullet list

Postby mtran » Tue May 28, 2019 5:21 am

Got it, thanks again!
LibreOffice 6.1.5.2 on Windows 10
mtran
 
Posts: 3
Joined: Thu May 23, 2019 9:27 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 2 guests