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 22import uno 23 24# a UNO struct later needed to create a document 25from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK 26from com.sun.star.text.TextContentAnchorType import AS_CHARACTER 27from com.sun.star.awt import Size 28 29from com.sun.star.lang import XMain 30 31def insertTextIntoCell( table, cellName, text, color ): 32 tableText = table.getCellByName( cellName ) 33 cursor = tableText.createTextCursor() 34 cursor.setPropertyValue( "CharColor", color ) 35 tableText.setString( text ) 36 37 38def createTable(): 39 """creates a new writer document and inserts a table with some data (also known as the SWriter sample)""" 40 ctx = uno.getComponentContext() 41 smgr = ctx.ServiceManager 42 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) 43 44 # open a writer document 45 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () ) 46 47 text = doc.Text 48 cursor = text.createTextCursor() 49 text.insertString( cursor, "The first line in the newly created text document.\n", 0 ) 50 text.insertString( cursor, "Now we are in the second line\n" , 0 ) 51 52 # create a text table 53 table = doc.createInstance( "com.sun.star.text.TextTable" ) 54 55 # with 4 rows and 4 columns 56 table.initialize( 4,4) 57 58 text.insertTextContent( cursor, table, 0 ) 59 rows = table.Rows 60 61 table.setPropertyValue( "BackTransparent", uno.Bool(0) ) 62 table.setPropertyValue( "BackColor", 13421823 ) 63 row = rows.getByIndex(0) 64 row.setPropertyValue( "BackTransparent", uno.Bool(0) ) 65 row.setPropertyValue( "BackColor", 6710932 ) 66 67 textColor = 16777215 68 69 insertTextIntoCell( table, "A1", "FirstColumn", textColor ) 70 insertTextIntoCell( table, "B1", "SecondColumn", textColor ) 71 insertTextIntoCell( table, "C1", "ThirdColumn", textColor ) 72 insertTextIntoCell( table, "D1", "SUM", textColor ) 73 74 values = ( (22.5,21.5,121.5), 75 (5615.3,615.3,-615.3), 76 (-2315.7,315.7,415.7) ) 77 table.getCellByName("A2").setValue(22.5) 78 table.getCellByName("B2").setValue(5615.3) 79 table.getCellByName("C2").setValue(-2315.7) 80 table.getCellByName("D2").setFormula("sum <A2:C2>") 81 82 table.getCellByName("A3").setValue(21.5) 83 table.getCellByName("B3").setValue(615.3) 84 table.getCellByName("C3").setValue(-315.7) 85 table.getCellByName("D3").setFormula("sum <A3:C3>") 86 87 table.getCellByName("A4").setValue(121.5) 88 table.getCellByName("B4").setValue(-615.3) 89 table.getCellByName("C4").setValue(415.7) 90 table.getCellByName("D4").setFormula("sum <A4:C4>") 91 92 93 cursor.setPropertyValue( "CharColor", 255 ) 94 cursor.setPropertyValue( "CharShadowed", uno.Bool(1) ) 95 96 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 97 text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 ) 98 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 99 100 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" ) 101 textFrame.setSize( Size(15000,400)) 102 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER ) 103 104 text.insertTextContent( cursor, textFrame, 0 ) 105 106 textInTextFrame = textFrame.getText() 107 cursorInTextFrame = textInTextFrame.createTextCursor() 108 textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 ) 109 textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0) 110 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 111 112 cursor.setPropertyValue( "CharColor", 65536 ) 113 cursor.setPropertyValue( "CharShadowed", uno.Bool(0) ) 114 115 text.insertString( cursor, " That's all for now !!" , 0 ) 116 117g_exportedScripts = createTable, 118