cid#1640468 Dereference after null check
[LibreOffice.git] / odk / examples / python / Text / TextReplace.py
blobc1b94a58c49ea5391eec81cebb35723d167cd7cd
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import officehelper
11 import sys
12 import traceback
14 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
17 def get_desktop():
18 desktop = None
19 try:
20 remote_context = officehelper.bootstrap()
21 srv_mgr = remote_context.getServiceManager()
22 if srv_mgr is None:
23 print("Can't create a desktop. No connection, no remote office servicemanager available!")
24 else:
25 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
26 except Exception:
27 traceback.print_exc()
28 sys.exit(1)
29 return desktop
32 def main():
33 desktop = get_desktop()
34 if desktop is None:
35 return
37 print("Opening an empty Writer document")
39 try:
40 doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple())
41 except Exception:
42 traceback.print_exc()
43 sys.exit(1)
45 create_example_data(doc)
47 british_words = ["colour", "neighbour", "centre", "behaviour", "metre", "through"]
48 us_words = ["color", "neighbor", "center", "behavior", "meter", "thru"]
50 try:
51 replace_descriptor = doc.createReplaceDescriptor()
52 print("Change all occurrences of ...")
53 for british_word, us_word in zip(british_words, us_words):
54 replace_descriptor.setSearchString(british_word)
55 replace_descriptor.setReplaceString(us_word)
56 # Replace all words
57 replaced_cnt = doc.replaceAll(replace_descriptor)
58 if replaced_cnt > 0:
59 print("Replaced", british_word, "with", us_word)
60 except Exception:
61 traceback.print_exc()
63 print("Done")
66 def create_example_data(doc):
67 try:
68 text = doc.getText()
69 cursor = text.createTextCursor()
70 text.insertString(cursor, "He nervously looked all around. Suddenly he saw his ", False)
72 text.insertString(cursor, "neighbour ", True)
73 cursor.setPropertyValue("CharColor", 255) # Set the word blue
75 cursor.gotoEnd(False) # Go to last character
76 cursor.setPropertyValue("CharColor", 0)
77 content = (
78 "in the alley. Like lightning he darted off to the left and disappeared between the "
79 "two warehouses almost falling over the trash can lying in the "
81 text.insertString(cursor, content, False)
83 text.insertString(cursor, "centre ", True)
84 cursor.setPropertyValue("CharColor", 255) # Set the word blue
86 cursor.gotoEnd(False) # Go to last character
87 cursor.setPropertyValue("CharColor", 0)
88 text.insertString(cursor, "of the sidewalk.", False)
90 text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
91 content = (
92 "He tried to nervously tap his way along in the inky darkness and suddenly stiffened: "
93 "it was a dead-end, he would have to go back the way he had come."
95 text.insertString(cursor, content, False)
96 cursor.gotoStart(False)
97 except Exception:
98 traceback.print_exc()
101 if __name__ == "__main__":
102 main()
104 # vim: set shiftwidth=4 softtabstop=4 expandtab: