[Solved] Python API to replace text with bullet list
Posted: 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:
So basically, replace my placeholder with a multi line list prepended with the bullet character. I greatly appreciate any help or suggestions!
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
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)