bump product version to 5.0.4.1
[LibreOffice.git] / pyuno / demo / swriter.py
blob865160a118512fea42cc5a7eca18db1e69451321
1 # -*- tab-width: 4; indent-tabs-mode: nil; c-basic-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/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 # bootstrap uno component context
21 import uno
22 import unohelper
24 from com.sun.star.lang import IllegalArgumentException
26 # a UNO struct later needed to create a document
27 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
28 from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
29 from com.sun.star.awt import Size
32 def insertTextIntoCell( table, cellName, text, color ):
33 tableText = table.getCellByName( cellName )
34 cursor = tableText.createTextCursor()
35 cursor.setPropertyValue( "CharColor", color )
36 tableText.setString( text )
38 localContext = uno.getComponentContext()
40 resolver = localContext.ServiceManager.createInstanceWithContext(
41 "com.sun.star.bridge.UnoUrlResolver", localContext )
43 smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
44 remoteContext = smgr.getPropertyValue( "DefaultContext" )
46 #remoteContext = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
47 #smgr = remoteContext.ServiceManager
49 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext)
51 # open a writer document
52 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
54 text = doc.Text
55 cursor = text.createTextCursor()
56 text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
57 text.insertString( cursor, "Now we are in the second line\n" , 0 )
59 # create a text table
60 table = doc.createInstance( "com.sun.star.text.TextTable" )
62 # with 4 rows and 4 columns
63 table.initialize(4, 4)
65 text.insertTextContent( cursor, table, 0 )
66 rows = table.Rows
68 table.setPropertyValue( "BackTransparent", False )
69 table.setPropertyValue( "BackColor", 13421823 )
70 row = rows.getByIndex(0)
71 row.setPropertyValue( "BackTransparent", False )
72 row.setPropertyValue( "BackColor", 6710932 )
74 textColor = 16777215
76 insertTextIntoCell( table, "A1", "FirstColumn", textColor )
77 insertTextIntoCell( table, "B1", "SecondColumn", textColor )
78 insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
79 insertTextIntoCell( table, "D1", "SUM", textColor )
81 table.getCellByName("A2").setValue(22.5)
82 table.getCellByName("B2").setValue(5615.3)
83 table.getCellByName("C2").setValue(-2315.7)
84 table.getCellByName("D2").setFormula("sum <A2:C2>")
86 table.getCellByName("A3").setValue(21.5)
87 table.getCellByName("B3").setValue(615.3)
88 table.getCellByName("C3").setValue(-315.7)
89 table.getCellByName("D3").setFormula("sum <A3:C3>")
91 table.getCellByName("A4").setValue(121.5)
92 table.getCellByName("B4").setValue(-615.3)
93 table.getCellByName("C4").setValue(415.7)
94 table.getCellByName("D4").setFormula("sum <A4:C4>")
97 cursor.setPropertyValue( "CharColor", 255 )
98 cursor.setPropertyValue( "CharShadowed", True )
100 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
101 text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 )
102 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
104 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
105 textFrame.setSize( Size(15000,400))
106 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
109 text.insertTextContent( cursor, textFrame, 0 )
111 textInTextFrame = textFrame.getText()
112 cursorInTextFrame = textInTextFrame.createTextCursor()
113 textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
114 textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0)
115 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
117 cursor.setPropertyValue( "CharColor", 65536 )
118 cursor.setPropertyValue( "CharShadowed", False )
120 text.insertString( cursor, " That's all for now!" , 0 )
122 # vim:set shiftwidth=4 softtabstop=4 expandtab: