Update git submodules
[LibreOffice.git] / odk / examples / python / Text / WriterSelector.py
blob3b659fd4394739e7297ccf0e4b5b0a3696c0cf51
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import sys
11 import traceback
12 import officehelper
15 def main():
16 try:
17 remote_context = officehelper.bootstrap()
18 print("Connected to a running office ...")
19 srv_mgr = remote_context.getServiceManager()
20 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
22 print("Opening an empty Writer document")
23 doc_url = "private:factory/swriter"
24 doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())
26 text = doc.getText()
27 text.setString("Please select something in this text and press then \"return\" in the shell "
28 "where you have started the example.\n")
30 # Returned object supports service com.sun.star.text.TextDocumentView and com.sun.star.view.OfficeDocumentView
31 # Both of them implements interface com::sun::star::view::XViewSettingsSupplier
32 obj = doc.getCurrentController()
33 obj.getViewSettings().setPropertyValue("ZoomType", 0)
35 print()
36 input("Please select something in the test document and press "
37 "then \"return\" to continues the example ... ")
39 frame = desktop.getCurrentFrame()
40 selection = frame.getController().getSelection()
42 if selection.supportsService("com.sun.star.text.TextRanges"):
43 for selected in selection:
44 print("You have selected a text range:", f'"{selected.getString()}".')
46 if selection.supportsService("com.sun.star.text.TextGraphicObject"):
47 print("You have selected a graphics.")
49 if selection.supportsService("com.sun.star.text.TexttableCursor"):
50 print("You have selected a text table.")
52 # When object returned from loadComponentFromURL does not support a service
53 # that implements XCloseable interface, fallback to call
54 # XComponent.dispose.
55 try:
56 doc.close(False)
57 except Exception:
58 doc.dispose()
59 except Exception:
60 traceback.print_exc()
61 sys.exit(1)
64 if __name__ == "__main__":
65 main()
67 # vim: set shiftwidth=4 softtabstop=4 expandtab: