Ask Your Question
0

How do I find and replace many variable at once?

asked 2018-03-05 01:14:12 +0200

ddelma2 gravatar image

Having a text like A B C D... I would like to replace let's say A -> 1, B ->2 etc. The number of patterns to be changed is high. That's why I would like to instruct LibreOffice to do it, but how?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2018-03-05 15:43:25 +0200

Jim K gravatar image

updated 2018-03-05 15:55:12 +0200

Here is a Python macro that works in both Writer and Calc. Use APSO to make it easier to create and run Python macros.

def replace_multiple():
    oDoc = XSCRIPTCONTEXT.getDocument()
    if oDoc.supportsService("com.sun.star.text.TextDocument"):
        replace_multiple_in(oDoc)
    elif oDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
        for oSheet in oDoc.getSheets():
            replace_multiple_in(oSheet)

def replace_multiple_in(xReplaceable):
    # Note: If replacement strings are irregular, use a Python dictionary instead of 'ABCD'.
    for letter in 'ABCD':
        oReplace = xReplaceable.createReplaceDescriptor()
        oReplace.setSearchString(letter)
        oReplace.setReplaceString(ord(letter) - ord('A') + 1)
        oReplace.SearchRegularExpression = True
        xReplaceable.replaceAll(oReplace)

g_exportedScripts = replace_multiple,
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-03-05 01:14:12 +0200

Seen: 63 times

Last updated: Mar 05 '18