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/.
14 from com
.sun
.star
.text
.ControlCharacter
import PARAGRAPH_BREAK
20 remote_context
= officehelper
.bootstrap()
21 srv_mgr
= remote_context
.getServiceManager()
23 print("Can't create a desktop. No connection, no remote office servicemanager available!")
25 desktop
= srv_mgr
.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context
)
33 desktop
= get_desktop()
37 print("Opening an empty Writer document")
40 doc
= desktop
.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple())
45 create_example_data(doc
)
47 british_words
= ["colour", "neighbour", "centre", "behaviour", "metre", "through"]
48 us_words
= ["color", "neighbor", "center", "behavior", "meter", "thru"]
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
)
57 replaced_cnt
= doc
.replaceAll(replace_descriptor
)
59 print("Replaced", british_word
, "with", us_word
)
66 def create_example_data(doc
):
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)
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)
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)
101 if __name__
== "__main__":
104 # vim: set shiftwidth=4 softtabstop=4 expandtab: