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/.
12 from com
.sun
.star
.connection
import NoConnectException
13 from os
.path
import isfile
, abspath
, basename
19 raise argparse
.ArgumentTypeError("{} could not be opened".format(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",
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
)
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
)
55 url
= uno
.systemPathToFileUrl(abspath(args
.path
))
57 url
= "private:factory/swriter"
59 url
= "private:factory/scalc"
61 url
= "private:factory/sdraw"
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: