Ask Your Question

Calc: How to Find and Replace with Python

asked 2016-07-15 16:34:36 +0100

elsi gravatar image

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("", 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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-07-16 17:08:57 +0100

mauricio gravatar image

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

Try with this code:

Test with some different values in replace and see result.

Best regards

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-07-15 16:34:36 +0100

Seen: 394 times

Last updated: Jul 16 '16