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
.awt
.FontWeight
import BOLD
15 from com
.sun
.star
.beans
.PropertyState
import AMBIGUOUS_VALUE
16 from com
.sun
.star
.beans
.PropertyState
import DEFAULT_VALUE
17 from com
.sun
.star
.beans
.PropertyState
import DIRECT_VALUE
18 from com
.sun
.star
.text
.ControlCharacter
import PARAGRAPH_BREAK
24 remote_context
= officehelper
.bootstrap()
25 srv_mgr
= remote_context
.getServiceManager()
27 print("Can't create a desktop. No connection, no remote office servicemanager available!")
29 desktop
= srv_mgr
.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context
)
37 desktop
= get_desktop()
42 doc
= desktop
.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple())
45 cursor
= text
.createTextCursor()
47 text
.insertString(cursor
, "Headline", False)
48 text
.insertControlCharacter(cursor
, PARAGRAPH_BREAK
, False)
49 text
.insertString(cursor
, "A very short paragraph for illustration only", False)
51 # Start 'Hard formatting'
52 # the text range not the cursor contains the 'parastyle' property
53 text_range
= text
.getEnd()
54 text_range
.gotoStart(False)
55 text_range
.gotoEndOfParagraph(True)
57 # Later, we will go through words in this text range
58 text_range
= text_range
.getText().getStart()
59 # Display the current text attributes
60 print("Parastyle:", text_range
.getPropertyValue("ParaStyleName"))
61 print("Fontname: ", text_range
.getPropertyValue("CharFontName"))
62 print("Weight: ", text_range
.getPropertyValue("CharWeight"))
65 text_range
.gotoNextWord(False)
66 text_range
.gotoNextWord(False)
67 text_range
.gotoEndOfWord(True)
68 # And set text attributes
69 text_range
.setPropertyValue("CharWeight", BOLD
)
70 text_range
.setPropertyValue("CharColor", 255)
71 # Then, display the text attributes
72 print("Parastyle:", text_range
.getPropertyValue("ParaStyleName"))
73 print("Fontname: ", text_range
.getPropertyValue("CharFontName"))
74 print("Weight: ", text_range
.getPropertyValue("CharWeight"))
76 # the PropertyState contains information where the attribute is set,
77 # is a text part hard formatted or not.
78 check_property_state(text_range
, text_range
.getPropertyState("CharWeight"))
80 print("Increase the selection with three characters")
81 text_range
.goRight(3, True)
82 check_property_state(text_range
, text_range
.getPropertyState("CharWeight"))
84 print("Set the default value on the selection")
85 text_range
.setPropertyToDefault("CharWeight")
87 check_property_state(text_range
, text_range
.getPropertyState("CharWeight"))
95 def check_property_state(text_range
, prop_state
):
96 if prop_state
== DIRECT_VALUE
:
97 print("-> The selection", f
"'{text_range.getString()}'", "completely hard formatted")
98 elif prop_state
== DEFAULT_VALUE
:
99 print("-> The selection", f
"'{text_range.getString()}'", "isn't hard formatted")
100 elif prop_state
== AMBIGUOUS_VALUE
:
101 print("-> The selection", f
"'{text_range.getString()}'", "isn't completely hard formatted")
103 print("No PropertyState found")
106 if __name__
== "__main__":
109 # vim: set shiftwidth=4 softtabstop=4 expandtab: