Update ooo320-m1
[ooovba.git] / pyuno / demo / swritercomp.py
blob6f8f30607bd2d69c488468f85f2eb0fa618f510d
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.
4 import unohelper
5 import uno
7 # a UNO struct later needed to create a document
8 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
9 from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
10 from com.sun.star.awt import Size
12 from com.sun.star.lang import XMain
14 def insertTextIntoCell( table, cellName, text, color ):
15 tableText = table.getCellByName( cellName )
16 cursor = tableText.createTextCursor()
17 cursor.setPropertyValue( "CharColor", color )
18 tableText.setString( text )
20 # the UNO component
21 # implementing the interface com.sun.star.lang.XMain
22 # unohelper.Base implements the XTypeProvider interface
23 class SWriterComp(XMain,unohelper.Base):
24 def __init__( self, ctx ):
25 self.ctx = ctx
27 # implementation for XMain.run( [in] sequence< any > )
28 def run( self,args ):
30 ctx = self.ctx
31 smgr = ctx.ServiceManager
32 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
34 # open a writer document
35 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
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 )
42 # create a text table
43 table = doc.createInstance( "com.sun.star.text.TextTable" )
45 # with 4 rows and 4 columns
46 table.initialize( 4,4)
48 text.insertTextContent( cursor, table, 0 )
49 rows = table.Rows
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 )
57 textColor = 16777215
59 insertTextIntoCell( table, "A1", "FirstColumn", textColor )
60 insertTextIntoCell( table, "B1", "SecondColumn", textColor )
61 insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
62 insertTextIntoCell( table, "D1", "SUM", textColor )
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>")
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>")
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>")
83 cursor.setPropertyValue( "CharColor", 255 )
84 cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )
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 )
90 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
91 textFrame.setSize( Size(15000,400))
92 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
94 text.insertTextContent( cursor, textFrame, 0 )
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 )
102 cursor.setPropertyValue( "CharColor", 65536 )
103 cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )
105 text.insertString( cursor, " That's all for now !!" , 0 )
106 return 0
109 # pythonloader looks for a static g_ImplementationHelper variable
110 g_ImplementationHelper = unohelper.ImplementationHelper()
111 g_ImplementationHelper.addImplementation( \
112 SWriterComp,"org.openoffice.comp.pyuno.swriter",("org.openoffice.demo.SWriter",),)