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