diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /odk/examples/python/Text/TextReplace.py | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'odk/examples/python/Text/TextReplace.py')
-rw-r--r-- | odk/examples/python/Text/TextReplace.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/odk/examples/python/Text/TextReplace.py b/odk/examples/python/Text/TextReplace.py new file mode 100644 index 0000000000..78d301998f --- /dev/null +++ b/odk/examples/python/Text/TextReplace.py @@ -0,0 +1,106 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +import officehelper +import sys +import traceback + +from com.sun.star.awt import Size +from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK +from com.sun.star.text.TextContentAnchorType import AS_CHARACTER + + +def get_desktop(): + desktop = None + try: + remote_context = officehelper.bootstrap() + srv_mgr = remote_context.getServiceManager() + if srv_mgr is None: + print("Can't create a desktop. No connection, no remote office servicemanager available!") + else: + desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context) + except: + traceback.print_exc() + sys.exit(1) + return desktop + + +def main(): + desktop = get_desktop() + if desktop is None: + return + + print("Opening an empty Writer document") + + try: + doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple()) + except: + traceback.print_exc() + sys.exit(1) + + create_example_data(doc) + + british_words = ["colour", "neighbour", "centre", "behaviour", "metre", "through"] + us_words = ["color", "neighbor", "center", "behavior", "meter", "thru"] + + try: + replace_descriptor = doc.createReplaceDescriptor() + print("Change all occurrences of ...") + for british_word, us_word in zip(british_words, us_words): + replace_descriptor.setSearchString(british_word) + replace_descriptor.setReplaceString(us_word) + # Replace all words + replaced_cnt = doc.replaceAll(replace_descriptor) + if replaced_cnt > 0: + print("Replaced", british_word, "with", us_word) + except: + traceback.print_exc() + + print("Done") + + +def create_example_data(doc): + try: + text = doc.getText() + cursor = text.createTextCursor() + text.insertString(cursor, "He nervously looked all around. Suddenly he saw his ", False) + + text.insertString(cursor, "neighbour ", True) + cursor.setPropertyValue("CharColor", 255) # Set the word blue + + cursor.gotoEnd(False) # Go to last character + cursor.setPropertyValue("CharColor", 0) + content = ( + "in the alley. Like lightning he darted off to the left and disappeared between the " + "two warehouses almost falling over the trash can lying in the " + ) + text.insertString(cursor, content, False) + + text.insertString(cursor, "centre ", True) + cursor.setPropertyValue("CharColor", 255) # Set the word blue + + cursor.gotoEnd(False) # Go to last character + cursor.setPropertyValue("CharColor", 0) + text.insertString(cursor, "of the sidewalk.", False) + + text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False) + content = ( + "He tried to nervously tap his way along in the inky darkness and suddenly stiffened: " + "it was a dead-end, he would have to go back the way he had come." + ) + text.insertString(cursor, content, False) + cursor.gotoStart(False) + except: + traceback.print_exc() + + +if __name__ == "__main__": + main() + +# vim: set shiftwidth=4 softtabstop=4 expandtab: |