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
.text
.ControlCharacter
import PARAGRAPH_BREAK
20 remote_context
= officehelper
.bootstrap()
21 srv_mgr
= remote_context
.getServiceManager()
23 print("Can't create a desktop. No connection, no remote office servicemanager available!")
25 desktop
= srv_mgr
.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context
)
33 desktop
= get_desktop()
38 doc_url
= "private:factory/swriter"
39 doc
= desktop
.loadComponentFromURL(doc_url
, "_blank", 0, tuple())
42 cursor
= text
.createTextCursor()
45 cursor
.setPropertyValue("CharFontName", "Arial")
48 text
.insertString(cursor
, "Headline", False)
51 cursor
.setPropertyValue("CharFontName", "Liberation Sans")
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
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
)
104 if __name__
== "__main__":
108 # vim: set shiftwidth=4 softtabstop=4 expandtab: