Text Documents (Writer)
HTML Documents (Writer Web)
Spreadsheets (Calc)
Presentations (Impress)
Drawings (Draw)
Database Functionality (Base)
Formulae (Math)
Charts and Diagrams
Macros and Scripting
Office Installation
Common Help Topics
OneOffice Logo

Running Python Interactive Console

The Python interactive console, also known as Python interpreter or Python shell, provides programmers with a quick way to execute commands and try out and test code without creating a file. UNO objects introspection as well as Office Python modules documentation can be obtained from the terminal.

From a full-featured Office installed package, use either Basic or Python:

Using a Basic macro


        Sub interpreter-console
            Const UNIX = 4
            ps = CreateUnoService("com.sun.star.util.PathSettings")
            install-path = ConvertFromURL(ps.Module)
            cmd = IIF(GetGuiType()=UNIX,"x-terminal-emulator -e ","")
            Shell(cmd + install-path + GetPathSeparator() + "python" )
        End Sub
    

Using a Python macro


        # -*- coding: utf-8 -*-
        from --future-- import unicode-literals
           
        import uno, os, subprocess
            
        def interpreter-console():
            ctx = XSCRIPTCONTEXT.getComponentContext()
            smgr = ctx.getServiceManager()
            ps = smgr.createInstanceWithContext("com.sun.star.util.PathSettings", ctx)
            install-path = uno.fileUrlToSystemPath(ps.Module)
            pgm = install-path + os.sep + "python"  # Python shell/console path
            subprocess.Popen(pgm)  # Start Python interactive Shell
            
    

Example output

Python Interactive Console

Using the Terminal

From a Office copy included in a GNU/Linux platform, use the terminal as shown:

whereis or type terminal commands help locate Python interactive console:

user@computer:~$ type -p python3/usr/bin/python3 user@computer:~$ /usr/bin/python3 Python 3.7.5 (default, Nov 20 2019, 09:21:52)[GCC 9.2.1 20191008] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import uno>>> dir(uno)['Any', 'Bool', 'ByteSequence', 'Char', 'Enum', 'PY2', 'Type', '-ConstantGroup', '--builtin--', '--builtins--', '--cached--', '--doc--', '--file--', '--loader--', '--name--', '--package--', '--spec--', '-builtin-import', '-component-context', '-impl-getConstantGroupByName', '-uno-extract-printable-stacktrace', '-uno-import', '-uno-struct--eq--', '-uno-struct--getattr--', '-uno-struct--init--', '-uno-struct--ne--', '-uno-struct--repr--', '-uno-struct--setattr--', '-uno-struct--str--', 'absolutize', 'createUnoStruct', 'fileUrlToSystemPath', 'generateUuid', 'getClass', 'getComponentContext', 'getConstantByName', 'getCurrentContext', 'getTypeByName', 'invoke', 'isInterface', 'os', 'pyuno', 'setCurrentContext', 'six-string-types', 'socket', 'sys', 'systemPathToFileUrl', 'traceback', 'warnings']>>> exit()user@computer:~$

Alternative console

Use APSO extension console as an alternative:

APSO console