tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / DocumentHandling / DocumentLoader.py
bloba88f3ad0d724ad5bd00c5b2161e84de10b1132c2
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 argparse
11 import uno
12 from com.sun.star.connection import NoConnectException
13 from os.path import isfile, abspath, basename
14 from sys import argv
17 def is_file(value):
18 if not isfile(value):
19 raise argparse.ArgumentTypeError("{} could not be opened".format(value))
20 return value
23 PROG = "$OFFICE_PROGRAM_PATH/python {}".format(basename(argv[0]))
26 if __name__ == "__main__":
27 parser = argparse.ArgumentParser(prog=PROG)
28 parser.add_argument("--writer", action="store_true", required=False, help="Open an empty Writer document")
29 parser.add_argument("--calc", action="store_true", required=False, help="Open an empty Calc document")
30 parser.add_argument("--draw", action="store_true", required=False, help="Open an empty Draw document")
31 parser.add_argument("path",
32 type=is_file,
33 nargs="?",
34 help="Path to a document to load. If omitted, an empty document is opened accordingly.")
35 args = parser.parse_args()
37 # UNO component context for initializing the Python runtime
38 localContext = uno.getComponentContext()
40 # Create an instance of a service implementation
41 resolver = localContext.ServiceManager.createInstanceWithContext(
42 "com.sun.star.bridge.UnoUrlResolver", localContext)
44 try:
45 context = resolver.resolve(
46 "uno:socket,host=localhost,"
47 "port=2083;urp;StarOffice.ComponentContext")
48 except NoConnectException:
49 raise Exception("Error: cannot establish a connection to LibreOffice.")
51 desktop = context.ServiceManager.createInstanceWithContext(
52 "com.sun.star.frame.Desktop", context)
54 if args.path:
55 url = uno.systemPathToFileUrl(abspath(args.path))
56 elif args.writer:
57 url = "private:factory/swriter"
58 elif args.calc:
59 url = "private:factory/scalc"
60 elif args.draw:
61 url = "private:factory/sdraw"
62 else:
63 url = "private:factory/swriter"
65 # Load a LibreOffice document, and automatically display it on the screen
66 xComp = desktop.loadComponentFromURL(url, "_blank", 0, tuple([]))
68 # vim: set shiftwidth=4 softtabstop=4 expandtab: