Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / python / pythonSamples / TableSample.py
blob12ea326343ccbbdcde70a640b4dcf84fd1f5cf20
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import uno
21 # a UNO struct later needed to create a document
22 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
23 from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
24 from com.sun.star.awt import Size
26 from com.sun.star.lang import XMain
28 def insertTextIntoCell( table, cellName, text, color ):
29 tableText = table.getCellByName( cellName )
30 cursor = tableText.createTextCursor()
31 cursor.setPropertyValue( "CharColor", color )
32 tableText.setString( text )
35 def createTable():
36 """creates a new writer document and inserts a table with some data (also known as the SWriter sample)"""
37 ctx = uno.getComponentContext()
38 smgr = ctx.ServiceManager
39 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
41 # open a writer document
42 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
44 text = doc.Text
45 cursor = text.createTextCursor()
46 text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
47 text.insertString( cursor, "Now we are in the second line\n" , 0 )
49 # create a text table
50 table = doc.createInstance( "com.sun.star.text.TextTable" )
52 # with 4 rows and 4 columns
53 table.initialize( 4,4)
55 text.insertTextContent( cursor, table, 0 )
56 rows = table.Rows
58 table.setPropertyValue( "BackTransparent", uno.Bool(0) )
59 table.setPropertyValue( "BackColor", 13421823 )
60 row = rows.getByIndex(0)
61 row.setPropertyValue( "BackTransparent", uno.Bool(0) )
62 row.setPropertyValue( "BackColor", 6710932 )
64 textColor = 16777215
66 insertTextIntoCell( table, "A1", "FirstColumn", textColor )
67 insertTextIntoCell( table, "B1", "SecondColumn", textColor )
68 insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
69 insertTextIntoCell( table, "D1", "SUM", textColor )
71 values = ( (22.5,21.5,121.5),
72 (5615.3,615.3,-615.3),
73 (-2315.7,315.7,415.7) )
74 table.getCellByName("A2").setValue(22.5)
75 table.getCellByName("B2").setValue(5615.3)
76 table.getCellByName("C2").setValue(-2315.7)
77 table.getCellByName("D2").setFormula("sum <A2:C2>")
79 table.getCellByName("A3").setValue(21.5)
80 table.getCellByName("B3").setValue(615.3)
81 table.getCellByName("C3").setValue(-315.7)
82 table.getCellByName("D3").setFormula("sum <A3:C3>")
84 table.getCellByName("A4").setValue(121.5)
85 table.getCellByName("B4").setValue(-615.3)
86 table.getCellByName("C4").setValue(415.7)
87 table.getCellByName("D4").setFormula("sum <A4:C4>")
90 cursor.setPropertyValue( "CharColor", 255 )
91 cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )
93 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
94 text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 )
95 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
97 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
98 textFrame.setSize( Size(15000,400))
99 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
101 text.insertTextContent( cursor, textFrame, 0 )
103 textInTextFrame = textFrame.getText()
104 cursorInTextFrame = textInTextFrame.createTextCursor()
105 textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
106 textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0)
107 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
109 cursor.setPropertyValue( "CharColor", 65536 )
110 cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )
112 text.insertString( cursor, " That's all for now !!" , 0 )
114 g_exportedScripts = createTable,