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
.awt
import Size
15 from com
.sun
.star
.text
.ControlCharacter
import PARAGRAPH_BREAK
16 from com
.sun
.star
.text
.TextContentAnchorType
import AS_CHARACTER
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())
36 print("d", file=sys
.stderr
)
44 # Step 3: insert some text
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
56 cursor
.setPropertyValue("CharColor", 255)
57 cursor
.setPropertyValue("CharShadowed", True)
59 print("Couldn't change the color", file=sys
.stderr
)
63 text
.insertControlCharacter(cursor
, PARAGRAPH_BREAK
, False)
64 except Exception as e
:
65 print(f
"Couldn't insert break: {e}", file=sys
.stderr
)
68 print("Inserting colored Text")
69 text
.insertString(cursor
, " This is a colored Text - blue with shadow\n", False)
72 text
.insertControlCharacter(cursor
, PARAGRAPH_BREAK
, False)
73 except Exception as e
:
74 print(f
"Couldn't insert break: {e}", file=sys
.stderr
)
77 # Step 6: insert a text frame
78 insert_frame_with_text(doc
, text
, cursor
)
81 text
.insertControlCharacter(cursor
, PARAGRAPH_BREAK
, False)
82 except Exception as e
:
83 print(f
"Couldn't insert break: {e}", file=sys
.stderr
)
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)
97 def insert_table(doc
, text
, cursor
):
98 print("Inserting a text table")
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()
106 # initialize the text table with 4 columns an 4 rows
107 text_table
.initialize(4, 4)
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()
117 rows
= text_table
.getRows()
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")
140 ("B2", 5615.3, False),
141 ("C2", -2315.7, False),
142 ("D2", "sum <A2:C2>", True),
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
)
155 cell
.setFormula(value
)
160 def insert_frame_with_text(doc
, text
, cursor
):
162 text_frame
= doc
.createInstance("com.sun.star.text.TextFrame")
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()
172 # Change the AnchorType
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")
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()
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__":
210 # vim: set shiftwidth=4 softtabstop=4 expandtab: