tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / Text / StyleCreation.py
blob126189f47a88a140081bf1e3878f2fdd07d99fd7
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.awt.FontWeight import BOLD
15 from com.sun.star.style.BreakType import PAGE_AFTER
16 from com.sun.star.style.ParagraphAdjust import CENTER
18 NEW_STYLE_NAME = "myheading"
21 def create_style(component):
22 properties = component.createInstance("com.sun.star.style.ParagraphStyle")
23 properties.setPropertyValue("CharFontName", "Liberation Sans")
24 print("set name of the font to 'Liberation Sans'")
26 properties.setPropertyValue("CharHeight", float(36))
27 print("Change the height of th font to 36")
29 properties.setPropertyValue("CharWeight", float(BOLD))
30 print("set the font attribute 'Bold'")
32 properties.setPropertyValue("CharAutoKerning", True)
33 print("set the paragraph attribute 'AutoKerning'")
35 properties.setPropertyValue("ParaAdjust", CENTER)
36 print("set the paragraph adjust to LEFT")
38 properties.setPropertyValue("ParaFirstLineIndent", 0)
39 print("set the first line indent to 0 cm")
41 properties.setPropertyValue("BreakType", PAGE_AFTER)
42 print("set the paragraph attribute Breaktype to PageAfter")
44 # insert the new Paragraph style in the Paragraph style collection
45 style_families = component.getStyleFamilies()
46 paragraph_style_col = style_families["ParagraphStyles"]
47 paragraph_style_col[NEW_STYLE_NAME] = properties
48 print("create new paragraph style, with the values from the Propertyset")
51 def apply_style(component):
52 text_range = component.getText().getStart()
53 # change the value from the property 'ParaStyle' to apply the Paragraph style
54 # To run the sample with StarOffice 5.2 you'll have to change
55 # 'ParaStyleName' to 'ParaStyle' in the next line
56 text_range.setPropertyValue("ParaStyleName", NEW_STYLE_NAME)
57 print("apply the new paragraph style")
60 def get_desktop():
61 desktop = None
62 try:
63 remote_context = officehelper.bootstrap()
64 srv_mgr = remote_context.getServiceManager()
65 if srv_mgr is None:
66 print("Can't create a desktop. No connection, no remote office servicemanager available!")
67 else:
68 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
69 except Exception:
70 traceback.print_exc()
71 sys.exit(1)
72 return desktop
75 def main():
76 desktop = get_desktop()
77 if desktop is None:
78 return
80 try:
81 doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple())
82 create_style(doc)
83 apply_style(doc)
84 except Exception:
85 traceback.print_exc()
86 sys.exit(1)
88 print("Done")
91 if __name__ == "__main__":
92 main()
94 # vim: set shiftwidth=4 softtabstop=4 expandtab: