1# just a simple copy of the swriter.py demo, but implemented as a component. The advantage is, 2# that the component may run within the office process which may give a performance improvement. 3 4import unohelper 5import uno 6 7# a UNO struct later needed to create a document 8from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK 9from com.sun.star.text.TextContentAnchorType import AS_CHARACTER 10from com.sun.star.awt import Size 11 12from com.sun.star.lang import XMain 13 14def insertTextIntoCell( table, cellName, text, color ): 15 tableText = table.getCellByName( cellName ) 16 cursor = tableText.createTextCursor() 17 cursor.setPropertyValue( "CharColor", color ) 18 tableText.setString( text ) 19 20# the UNO component 21# implementing the interface com.sun.star.lang.XMain 22# unohelper.Base implements the XTypeProvider interface 23class SWriterComp(XMain,unohelper.Base): 24 def __init__( self, ctx ): 25 self.ctx = ctx 26 27 # implementation for XMain.run( [in] sequence< any > ) 28 def run( self,args ): 29 30 ctx = self.ctx 31 smgr = ctx.ServiceManager 32 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) 33 34 # open a writer document 35 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () ) 36 37 text = doc.Text 38 cursor = text.createTextCursor() 39 text.insertString( cursor, "The first line in the newly created text document.\n", 0 ) 40 text.insertString( cursor, "Now we are in the second line\n" , 0 ) 41 42 # create a text table 43 table = doc.createInstance( "com.sun.star.text.TextTable" ) 44 45 # with 4 rows and 4 columns 46 table.initialize( 4,4) 47 48 text.insertTextContent( cursor, table, 0 ) 49 rows = table.Rows 50 51 table.setPropertyValue( "BackTransparent", uno.Bool(0) ) 52 table.setPropertyValue( "BackColor", 13421823 ) 53 row = rows.getByIndex(0) 54 row.setPropertyValue( "BackTransparent", uno.Bool(0) ) 55 row.setPropertyValue( "BackColor", 6710932 ) 56 57 textColor = 16777215 58 59 insertTextIntoCell( table, "A1", "FirstColumn", textColor ) 60 insertTextIntoCell( table, "B1", "SecondColumn", textColor ) 61 insertTextIntoCell( table, "C1", "ThirdColumn", textColor ) 62 insertTextIntoCell( table, "D1", "SUM", textColor ) 63 64 values = ( (22.5,21.5,121.5), 65 (5615.3,615.3,-615.3), 66 (-2315.7,315.7,415.7) ) 67 table.getCellByName("A2").setValue(22.5) 68 table.getCellByName("B2").setValue(5615.3) 69 table.getCellByName("C2").setValue(-2315.7) 70 table.getCellByName("D2").setFormula("sum <A2:C2>") 71 72 table.getCellByName("A3").setValue(21.5) 73 table.getCellByName("B3").setValue(615.3) 74 table.getCellByName("C3").setValue(-315.7) 75 table.getCellByName("D3").setFormula("sum <A3:C3>") 76 77 table.getCellByName("A4").setValue(121.5) 78 table.getCellByName("B4").setValue(-615.3) 79 table.getCellByName("C4").setValue(415.7) 80 table.getCellByName("D4").setFormula("sum <A4:C4>") 81 82 83 cursor.setPropertyValue( "CharColor", 255 ) 84 cursor.setPropertyValue( "CharShadowed", uno.Bool(1) ) 85 86 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 87 text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 ) 88 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 89 90 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" ) 91 textFrame.setSize( Size(15000,400)) 92 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER ) 93 94 text.insertTextContent( cursor, textFrame, 0 ) 95 96 textInTextFrame = textFrame.getText() 97 cursorInTextFrame = textInTextFrame.createTextCursor() 98 textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 ) 99 textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0) 100 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 101 102 cursor.setPropertyValue( "CharColor", 65536 ) 103 cursor.setPropertyValue( "CharShadowed", uno.Bool(0) ) 104 105 text.insertString( cursor, " That's all for now !!" , 0 ) 106 return 0 107 108 109# pythonloader looks for a static g_ImplementationHelper variable 110g_ImplementationHelper = unohelper.ImplementationHelper() 111g_ImplementationHelper.addImplementation( \ 112 SWriterComp,"org.openoffice.comp.pyuno.swriter",("org.openoffice.demo.SWriter",),) 113