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

Input/Output to Screen

Python standard output file is not available when running Python macros from Tools – Macros - Run Macro... menu. Presenting the output of a module requires the Python interactive console. Features such as input(), print(), repr() and str() are available from the Python shell.

The Alternative Python Script Organizer (APSO) extension offers a msgbox() function out of its apso-utils module.

Office Basic proposes InputBox(), Msgbox() and Print() screen I/O functions. Python alternatives exist relying either on Office API Abstract Windowing Toolkit, either on Python to Basic function calls. The latter proposes a syntax that is intentionally close to that of Basic, and uses a Python module next to a Basic module. The API Scripting Framework is used to perform Basic, BeanShell, JavaScript and Python inter-languages function calls.

Python syntax:

MsgBox(txt, buttons=0, title=None) InputBox(txt, title=None, default=None) Print(txt)

Examples:

>>> import screen-io as ui

>>> reply = ui.InputBox('Please enter a phrase', title='Dear user', defaultValue="here..")

>>> rc = ui.MsgBox(reply, title="Confirmation of phrase")

>>> age = ui.InputBox('How old are you?', title="Hi")

>>> ui.Print(age)

Installation:

  • Copy screen-io Python module in My macros within /Scripts/python/pythonpath,
  • Copy uiScripts Basic module in My macros Standard Basic library,
  • Restart Office.

screen-io Python module


        # -*- coding: utf-8 -*-
        from --future-- import unicode-literals
        
        def MsgBox(prompt: str, buttons=0, title='Office') -> int:
            """ Displays a dialogue box containing a message and returns a value."""
            xScript = -getScript("-MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='Office', defaultValue='') -> str:
            """ Displays a prompt in a dialogue box at which the user can enter text."""
            xScript = -getScript("-InputBox")
            res = xScript.invoke((prompt,title,defaultValue), (), ())
            return res[0]
        
        def Print(message: str):
            """Outputs the specified strings or numeric expressions in a dialogue box."""
            xScript = -getScript("-Print")
            xScript.invoke((message,), (), ())
        
        import uno
        from com.sun.star.script.provider import XScript
        def -getScript(script: str, library='Standard', module='uiScripts') -> XScript:
            sm = uno.getComponentContext().ServiceManager
            mspf = sm.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", uno.getComponentContext())
            scriptPro = mspf.createScriptProvider("")
            scriptName = "vnd.sun.star.script:"+library+"."+module+"."+script+"?language=Basic&location=application"
            xScript = scriptPro.getScript(scriptName)
            return xScript
    

uiScripts Basic module


        Option Explicit
        Private Function -MsgBox( prompt As String, Optional buttons As Integer, -
                Optional title As String ) As Integer
            -MsgBox = MsgBox( prompt, buttons, title )
        End Function
        Private Function -InputBox( prompt As String, Optional title As String, -
                Optional default As String) As String
            -InputBox = InputBox( prompt, title, default )
        End Function
        Private Sub -Print( msg As String )
            Print msg
        End Sub