xref: /aoo4110/main/pyuno/demo/swriter.py (revision b1cdbd2c)
1# *************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20# *************************************************************
21
22
23# bootstrap uno component context
24import uno
25import unohelper
26
27
28# a UNO struct later needed to create a document
29from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
30from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
31from com.sun.star.awt import Size
32
33
34def insertTextIntoCell( table, cellName, text, color ):
35    tableText = table.getCellByName( cellName )
36    cursor = tableText.createTextCursor()
37    cursor.setPropertyValue( "CharColor", color )
38    tableText.setString( text )
39
40localContext = uno.getComponentContext()
41
42resolver = localContext.ServiceManager.createInstanceWithContext(
43				"com.sun.star.bridge.UnoUrlResolver", localContext )
44
45smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
46remoteContext = smgr.getPropertyValue( "DefaultContext" )
47
48#remoteContext = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
49#smgr = remoteContext.ServiceManager
50
51desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext)
52
53# open a writer document
54doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
55
56text = doc.Text
57cursor = text.createTextCursor()
58text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
59text.insertString( cursor, "Now we are in the second line.\n" , 0 )
60
61# create a text table
62table = doc.createInstance( "com.sun.star.text.TextTable" )
63
64# with 4 rows and 4 columns
65table.initialize( 4,4)
66
67text.insertTextContent( cursor, table, 0 )
68rows = table.Rows
69
70table.setPropertyValue( "BackTransparent", uno.Bool(0) )
71table.setPropertyValue( "BackColor", 13421823 )
72row = rows.getByIndex(0)
73row.setPropertyValue( "BackTransparent", uno.Bool(0) )
74row.setPropertyValue( "BackColor", 6710932 )
75
76textColor = 16777215
77
78insertTextIntoCell( table, "A1", "FirstColumn", textColor )
79insertTextIntoCell( table, "B1", "SecondColumn", textColor )
80insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
81insertTextIntoCell( table, "D1", "SUM", textColor )
82
83values = ( (22.5,21.5,121.5),
84	   (5615.3,615.3,-615.3),
85	   (-2315.7,315.7,415.7) )
86table.getCellByName("A2").setValue(22.5)
87table.getCellByName("B2").setValue(5615.3)
88table.getCellByName("C2").setValue(-2315.7)
89table.getCellByName("D2").setFormula("sum <A2:C2>")
90
91table.getCellByName("A3").setValue(21.5)
92table.getCellByName("B3").setValue(615.3)
93table.getCellByName("C3").setValue(-315.7)
94table.getCellByName("D3").setFormula("sum <A3:C3>")
95
96table.getCellByName("A4").setValue(121.5)
97table.getCellByName("B4").setValue(-615.3)
98table.getCellByName("C4").setValue(415.7)
99table.getCellByName("D4").setFormula("sum <A4:C4>")
100
101
102cursor.setPropertyValue( "CharColor", 255 )
103cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )
104
105text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
106text.insertString( cursor, " This is a colored text - blue with shadow\n" , 0 )
107text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
108
109textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
110textFrame.setSize( Size(15000,400))
111textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
112
113
114text.insertTextContent( cursor, textFrame, 0 )
115
116textInTextFrame = textFrame.getText()
117cursorInTextFrame = textInTextFrame.createTextCursor()
118textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
119textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the frame raises.",0)
120text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
121
122cursor.setPropertyValue( "CharColor", 65536 )
123cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )
124
125text.insertString( cursor, " That's all for now!" , 0 )
126
127