1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# -*- 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.awt.FontWeight import BOLD
from com.sun.star.beans.PropertyState import AMBIGUOUS_VALUE
from com.sun.star.beans.PropertyState import DEFAULT_VALUE
from com.sun.star.beans.PropertyState import DIRECT_VALUE
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
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
try:
doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple())
text = doc.getText()
cursor = text.createTextCursor()
text.insertString(cursor, "Headline", False)
text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
text.insertString(cursor, "A very short paragraph for illustration only", False)
# Start 'Hard formatting'
# the text range not the cursor contains the 'parastyle' property
text_range = text.getEnd()
text_range.gotoStart(False)
text_range.gotoEndOfParagraph(True)
# Later, we will go through words in this text range
text_range = text_range.getText().getStart()
# Display the current text attributes
print("Parastyle:", text_range.getPropertyValue("ParaStyleName"))
print("Fontname: ", text_range.getPropertyValue("CharFontName"))
print("Weight: ", text_range.getPropertyValue("CharWeight"))
# Move around
text_range.gotoNextWord(False)
text_range.gotoNextWord(False)
text_range.gotoEndOfWord(True)
# And set text attributes
text_range.setPropertyValue("CharWeight", BOLD)
text_range.setPropertyValue("CharColor", 255)
# Then, display the text attributes
print("Parastyle:", text_range.getPropertyValue("ParaStyleName"))
print("Fontname: ", text_range.getPropertyValue("CharFontName"))
print("Weight: ", text_range.getPropertyValue("CharWeight"))
# the PropertyState contains information where the attribute is set,
# is a text part hard formatted or not.
check_property_state(text_range, text_range.getPropertyState("CharWeight"))
print("Increase the selection with three characters")
text_range.goRight(3, True)
check_property_state(text_range, text_range.getPropertyState("CharWeight"))
print("Set the default value on the selection")
text_range.setPropertyToDefault("CharWeight")
# Then, check again
check_property_state(text_range, text_range.getPropertyState("CharWeight"))
except:
traceback.print_exc()
sys.exit(1)
print("Done")
def check_property_state(text_range, prop_state):
if prop_state == DIRECT_VALUE:
print("-> The selection", f"'{text_range.getString()}'", "completely hard formatted")
elif prop_state == DEFAULT_VALUE:
print("-> The selection", f"'{text_range.getString()}'", "isn't hard formatted")
elif prop_state == AMBIGUOUS_VALUE:
print("-> The selection", f"'{text_range.getString()}'", "isn't completely hard formatted")
else:
print("No PropertyState found")
if __name__ == "__main__":
main()
# vim: set shiftwidth=4 softtabstop=4 expandtab:
|