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
|
# -*- 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.FontWeight import BOLD
from com.sun.star.style.BreakType import PAGE_AFTER
from com.sun.star.style.ParagraphAdjust import CENTER
NEW_STYLE_NAME = "myheading"
def create_style(component):
properties = component.createInstance("com.sun.star.style.ParagraphStyle")
properties.setPropertyValue("CharFontName", "Liberation Sans")
print("set name of the font to 'Liberation Sans'")
properties.setPropertyValue("CharHeight", float(36))
print("Change the height of th font to 36")
properties.setPropertyValue("CharWeight", float(BOLD))
print("set the font attribute 'Bold'")
properties.setPropertyValue("CharAutoKerning", True)
print("set the paragraph attribute 'AutoKerning'")
properties.setPropertyValue("ParaAdjust", CENTER)
print("set the paragraph adjust to LEFT")
properties.setPropertyValue("ParaFirstLineIndent", 0)
print("set the first line indent to 0 cm")
properties.setPropertyValue("BreakType", PAGE_AFTER)
print("set the paragraph attribute Breaktype to PageAfter")
# insert the new Paragraph style in the Paragraph style collection
style_families = component.getStyleFamilies()
paragraph_style_col = style_families["ParagraphStyles"]
paragraph_style_col[NEW_STYLE_NAME] = properties
print("create new paragraph style, with the values from the Propertyset")
def apply_style(component):
text_range = component.getText().getStart()
# change the value from the property 'ParaStyle' to apply the Paragraph style
# To run the sample with StarOffice 5.2 you'll have to change
# 'ParaStyleName' to 'ParaStyle' in the next line
text_range.setPropertyValue("ParaStyleName", NEW_STYLE_NAME)
print("apply the new paragraph style")
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 Exception:
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())
create_style(doc)
apply_style(doc)
except Exception:
traceback.print_exc()
sys.exit(1)
print("Done")
if __name__ == "__main__":
main()
# vim: set shiftwidth=4 softtabstop=4 expandtab:
|