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