Update git submodules
[LibreOffice.git] / odk / examples / python / Text / SWriter.py
blob9927c2647b99f6f65f82ef350213695cfb06cc26
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 import Size
15 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
16 from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
19 def main():
20 try:
21 # Step 1: bootstrap UNO and get the remote component context. The
22 # context can be used to get the service manager.
23 remote_context = officehelper.bootstrap()
24 print("Connected to a running office ...")
25 srv_mgr = remote_context.getServiceManager()
26 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
28 # Step 2: open an empty document. In this case it's a writer document.
29 # For this purpose an instance of com.sun.star.frame.Desktop is
30 # created. It's interface XDesktop provides the XComponentLoader,
31 # which is used to open the document via loadComponentFromURL
32 print("Opening an empty Writer document")
33 doc_url = "private:factory/swriter"
34 doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())
35 except Exception:
36 print("d", file=sys.stderr)
37 traceback.print_exc()
38 sys.exit(1)
40 generate(doc)
43 def generate(doc):
44 # Step 3: insert some text
45 text = doc.getText()
46 cursor = text.createTextCursor()
48 text.insertString(cursor, "The first line in the newly created text document.\n", False)
49 text.insertString(cursor, "Now we're in the second line\n", False)
51 # Step 4: insert a text table
52 insert_table(doc, text, cursor)
54 # Step 5: insert a colored text
55 try:
56 cursor.setPropertyValue("CharColor", 255)
57 cursor.setPropertyValue("CharShadowed", True)
58 except Exception:
59 print("Couldn't change the color", file=sys.stderr)
60 traceback.print_exc()
62 try:
63 text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
64 except Exception as e:
65 print(f"Couldn't insert break: {e}", file=sys.stderr)
66 traceback.print_exc()
68 print("Inserting colored Text")
69 text.insertString(cursor, " This is a colored Text - blue with shadow\n", False)
71 try:
72 text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
73 except Exception as e:
74 print(f"Couldn't insert break: {e}", file=sys.stderr)
75 traceback.print_exc()
77 # Step 6: insert a text frame
78 insert_frame_with_text(doc, text, cursor)
80 try:
81 text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
82 except Exception as e:
83 print(f"Couldn't insert break: {e}", file=sys.stderr)
84 traceback.print_exc()
86 try:
87 cursor.setPropertyValue("CharColor", 65536)
88 cursor.setPropertyValue("CharShadowed", False)
89 except Exception as e:
90 print(f"Couldn't change the color: {e}", file=sys.stderr)
92 text.insertString(cursor, " That's all for now !!", False)
94 print("done")
97 def insert_table(doc, text, cursor):
98 print("Inserting a text table")
99 try:
100 text_table = doc.createInstance("com.sun.star.text.TextTable")
101 except Exception as e:
102 print(f"Couldn't create instance of TextTable: {e}", file=sys.stderr)
103 traceback.print_exc()
104 return
106 # initialize the text table with 4 columns an 4 rows
107 text_table.initialize(4, 4)
109 try:
110 text.insertTextContent(cursor, text_table, False)
111 except Exception as e:
112 print(f"Couldn't insert the table: {e}", file=sys.stderr)
113 traceback.print_exc()
114 return
116 # Get the first row
117 rows = text_table.getRows()
118 first_row = rows[0]
120 try:
121 # Set properties of the text table
122 text_table.setPropertyValue("BackTransparent", False)
123 text_table.setPropertyValue("BackColor", 13421823)
124 # Set properties of the first row
125 first_row.setPropertyValue("BackTransparent", False)
126 first_row.setPropertyValue("BackColor", 6710932)
127 except Exception as e:
128 print(f"Couldn't change the color: {e}", file=sys.stderr)
129 traceback.print_exc()
131 print("Write text in the table headers")
132 insert_into_cell("A1", "FirstColumn", text_table)
133 insert_into_cell("B1", "SecondColumn", text_table)
134 insert_into_cell("C1", "ThirdColumn", text_table)
135 insert_into_cell("D1", "SUM", text_table)
137 print("Insert something in the text table")
138 data = (
139 ("A2", 22.5, False),
140 ("B2", 5615.3, False),
141 ("C2", -2315.7, False),
142 ("D2", "sum <A2:C2>", True),
143 ("A3", 21.5, False),
144 ("B3", 615.3, False),
145 ("C3", -315.7, False),
146 ("D3", "sum <A3:C3>", True),
147 ("A4", 121.5, False),
148 ("B4", -615.3, False),
149 ("C4", 415.7, False),
150 ("D4", "sum <A4:C4>", True),
152 for cell_name, value, is_formula in data:
153 cell = text_table.getCellByName(cell_name)
154 if is_formula:
155 cell.setFormula(value)
156 else:
157 cell.setValue(value)
160 def insert_frame_with_text(doc, text, cursor):
161 try:
162 text_frame = doc.createInstance("com.sun.star.text.TextFrame")
163 frame_size = Size()
164 frame_size.Height = 400
165 frame_size.Width = 15000
166 text_frame.setSize(frame_size)
167 except Exception as e:
168 print(f"Couldn't create instance: {e}", file=sys.stderr)
169 traceback.print_exc()
170 return
172 # Change the AnchorType
173 try:
174 text_frame.setPropertyValue("AnchorType", AS_CHARACTER)
175 except Exception as e:
176 print(f"Couldn't change the color: {e}", file=sys.stderr)
177 traceback.print_exc()
179 print("Insert the text frame")
181 try:
182 text.insertTextContent(cursor, text_frame, False)
183 except Exception as e:
184 print(f"Couldn't insert the frame: {e}", file=sys.stderr)
185 traceback.print_exc()
187 frame_text = text_frame.getText()
188 frame_cursor = frame_text.createTextCursor()
189 s = "The first line in the newly created text frame."
190 text_frame.insertString(frame_cursor, s, False)
191 s = "\nWith this second line the height of the frame raises."
192 text_frame.insertString(frame_cursor, s, False)
195 def insert_into_cell(cell_name: str, content: str, text_table):
196 cell = text_table.getCellByName(cell_name)
197 cursor = cell.createTextCursor()
198 try:
199 cursor.setPropertyValue("CharColor", 16777215)
200 except Exception as e:
201 print(f"Fail to set CharColor property: {e}", file=sys.stderr)
202 traceback.print_exc()
203 # inserting some Text
204 cell.setString(content)
207 if __name__ == "__main__":
208 main()
210 # vim: set shiftwidth=4 softtabstop=4 expandtab: