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?
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,