summaryrefslogtreecommitdiffstats
path: root/pyuno/demo/swritercomp.py
blob: 7c200cc7bea5f5c30716a9349e918ed6928140fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#

# just a simple copy of the swriter.py demo, but implemented as a component. The advantage is,
# that the component may run within the office process which may give a performance improvement.

import unohelper
import uno

# a UNO struct later needed to create a document
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
from com.sun.star.awt import Size

from com.sun.star.lang import XMain

def insertTextIntoCell( table, cellName, text, color ):
    tableText = table.getCellByName( cellName )
    cursor = tableText.createTextCursor()
    cursor.setPropertyValue( "CharColor", color )
    tableText.setString( text )

# the UNO component
# implementing the interface com.sun.star.lang.XMain
# unohelper.Base implements the XTypeProvider interface
class SWriterComp(XMain,unohelper.Base):
    def __init__( self, ctx ):
        self.ctx = ctx

    # implementation for XMain.run( [in] sequence< any > )
    def run( self,args ):
        ctx = self.ctx
        smgr = ctx.ServiceManager
        desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

        # open a writer document
        doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )

        text = doc.Text
        cursor = text.createTextCursor()
        text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
        text.insertString( cursor, "Now we are in the second line\n" , 0 )

        # create a text table
        table = doc.createInstance( "com.sun.star.text.TextTable" )

        # with 4 rows and 4 columns
        table.initialize( 4,4)

        text.insertTextContent( cursor, table, 0 )
        rows = table.Rows

        table.setPropertyValue( "BackTransparent", uno.Bool(0) )
        table.setPropertyValue( "BackColor", 13421823 )
        row = rows[0]
        row.setPropertyValue( "BackTransparent", uno.Bool(0) )
        row.setPropertyValue( "BackColor", 6710932 )

        textColor = 16777215

        insertTextIntoCell( table, "A1", "FirstColumn", textColor )
        insertTextIntoCell( table, "B1", "SecondColumn", textColor )
        insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
        insertTextIntoCell( table, "D1", "SUM", textColor )

        table.getCellByName("A2").setValue(22.5)
        table.getCellByName("B2").setValue(5615.3)
        table.getCellByName("C2").setValue(-2315.7)
        table.getCellByName("D2").setFormula("sum <A2:C2>")

        table.getCellByName("A3").setValue(21.5)
        table.getCellByName("B3").setValue(615.3)
        table.getCellByName("C3").setValue(-315.7)
        table.getCellByName("D3").setFormula("sum <A3:C3>")

        table.getCellByName("A4").setValue(121.5)
        table.getCellByName("B4").setValue(-615.3)
        table.getCellByName("C4").setValue(415.7)
        table.getCellByName("D4").setFormula("sum <A4:C4>")


        cursor.setPropertyValue( "CharColor", 255 )
        cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )

        text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
        text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 )
        text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )

        textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
        textFrame.setSize( Size(15000,400))
        textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )

        text.insertTextContent( cursor, textFrame, 0 )

        textInTextFrame = textFrame.getText()
        cursorInTextFrame = textInTextFrame.createTextCursor()
        textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
        textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0)
        text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )

        cursor.setPropertyValue( "CharColor", 65536 )
        cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )

        text.insertString( cursor, " That's all for now!" , 0 )
        return 0

# pythonloader looks for a static g_ImplementationHelper variable
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
    SWriterComp,"org.openoffice.comp.pyuno.swriter",("org.openoffice.demo.SWriter",),)

# vim: set shiftwidth=4 softtabstop=4 expandtab: