Calc: How to Find and Replace with Python

Hello Officers!

I’m in desperate need of your help. Since yesterday I try to code a Find and Replace macro (+ Regular Expressions) with Python.

I want to find “.+” and replace it with “&”.

That’s what I have up to now:

def search():

document = XSCRIPTCONTEXT.getDocument()
ctx = uno.getComponentContext()
sm = ctx.ServiceManager
dispatcher = sm.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
sheet = document.Sheets.getByIndex(0)
	
replace = sheet.createReplaceDescriptor()
replace.SearchRegularExpression = True
replace.SearchString = ".+"
replace.ReplaceString ="&"
return None

Unfortunately, it happens absolutely nothing.

I’m happy and thankful for every little hint, code example and idea.

In Calc, if you use regular expression, the ampersand (&) mean, replace with the same value found.

Try with this code: https://gitlab.com/snippets/22593

Test with some different values in replace and see result.

Best regards