summaryrefslogtreecommitdiff
path: root/odk/examples/python/Text/WriterSelector.py
blob: 3b659fd4394739e7297ccf0e4b5b0a3696c0cf51 (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
# -*- 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/.
#

import sys
import traceback
import officehelper


def main():
    try:
        remote_context = officehelper.bootstrap()
        print("Connected to a running office ...")
        srv_mgr = remote_context.getServiceManager()
        desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)

        print("Opening an empty Writer document")
        doc_url = "private:factory/swriter"
        doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())

        text = doc.getText()
        text.setString("Please select something in this text and press then \"return\" in the shell "
                       "where you have started the example.\n")

        # Returned object supports service com.sun.star.text.TextDocumentView and com.sun.star.view.OfficeDocumentView
        # Both of them implements interface com::sun::star::view::XViewSettingsSupplier
        obj = doc.getCurrentController()
        obj.getViewSettings().setPropertyValue("ZoomType", 0)

        print()
        input("Please select something in the test document and press "
              "then \"return\" to continues the example ... ")

        frame = desktop.getCurrentFrame()
        selection = frame.getController().getSelection()

        if selection.supportsService("com.sun.star.text.TextRanges"):
            for selected in selection:
                print("You have selected a text range:", f'"{selected.getString()}".')

        if selection.supportsService("com.sun.star.text.TextGraphicObject"):
            print("You have selected a graphics.")

        if selection.supportsService("com.sun.star.text.TexttableCursor"):
            print("You have selected a text table.")

        # When object returned from loadComponentFromURL does not support a service
        # that implements XCloseable interface, fallback to call
        # XComponent.dispose.
        try:
            doc.close(False)
        except Exception:
            doc.dispose()
    except Exception:
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()

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