tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / Text / StyleInitialization.py
blob84f67369a78176482d39eba674aea0fb5768396a
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.text.ControlCharacter import PARAGRAPH_BREAK
17 def get_desktop():
18 desktop = None
19 try:
20 remote_context = officehelper.bootstrap()
21 srv_mgr = remote_context.getServiceManager()
22 if srv_mgr is None:
23 print("Can't create a desktop. No connection, no remote office servicemanager available!")
24 else:
25 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
26 except Exception:
27 traceback.print_exc()
28 sys.exit(1)
29 return desktop
32 def main():
33 desktop = get_desktop()
34 if desktop is None:
35 return
37 try:
38 doc_url = "private:factory/swriter"
39 doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())
41 text = doc.getText()
42 cursor = text.createTextCursor()
44 try:
45 cursor.setPropertyValue("CharFontName", "Arial")
46 except Exception:
47 pass
48 text.insertString(cursor, "Headline", False)
50 try:
51 cursor.setPropertyValue("CharFontName", "Liberation Sans")
52 except Exception:
53 pass
54 text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
55 text.insertString(cursor, "A very short paragraph for illustration only", False)
57 # The text range not the cursor contains the 'ParaStyleName' property
58 text_range = text.getEnd()
59 # To run the sample with StarOffice 5.2 you'll have to change
60 # 'ParaStyleName' to 'ParaStyle' in the next line
61 print("Current Parastyle:", text_range.getPropertyValue("ParaStyleName"))
63 # There are two way to travel through the paragraphs, with a paragraph
64 # cursor, or an enumeration. You find both ways in this example
66 # The first way, with the paragraph cursor
67 # Object text_range supports interface com.sun.star.text.XParagraphCursor
68 text_range.gotoStart(False)
69 text_range.gotoEndOfParagraph(True)
71 # The second way, with the paragraph enumeration
72 paragraph_enumeration = text.createEnumeration()
73 while paragraph_enumeration.hasMoreElements():
74 paragraph = paragraph_enumeration.nextElement()
75 # Create a cursor from this paragraph
76 paragraph_cursor = paragraph.getAnchor().getText().createTextCursor()
78 # Goto the start and end of the paragraph
79 paragraph_cursor.gotoStart(False)
80 paragraph_cursor.gotoEnd(True)
82 portion_enumeration = paragraph.createEnumeration()
83 while portion_enumeration.hasMoreElements():
84 word = portion_enumeration.nextElement()
85 print("Content of the paragraph:", word.getString())
87 # Find a paragraph style by a specific font name and apply the found
88 # style to paragraph.
89 style_families = doc.getStyleFamilies()
90 styles = style_families["ParagraphStyles"]
91 for style_name in styles.getElementNames():
92 style = styles[style_name]
93 font_name = style.getPropertyValue("CharFontName").lower()
94 if font_name == "liberation mono":
95 text_range.setPropertyValue("ParaStyleName", style_name)
96 print("Apply the paragraph style:", style_name)
97 break
98 except Exception:
99 traceback.print_exc()
101 print("Done")
104 if __name__ == "__main__":
105 main()
108 # vim: set shiftwidth=4 softtabstop=4 expandtab: