Ein-/Ausgabe auf Bildschirm

Die Python-Standardausgabedatei ist nicht verfügbar, wenn Python-Makros über Extras - Makros - Makro ausführen... ausgeführt werden. Für die Darstellung der Ausgabe eines Moduls ist die interaktive Python-Konsole erforderlich. Funktionen wie input(), print(), repr() und str() stehen in der Python-Shell zur Verfügung.

Tippsymbol

Die Erweiterung Alternative Python Script Organizer (APSO) bietet eine Funktion msgbox() aus ihrem Modul apso_utils.


LibreOffice Basic bietet die E/A-Bildschirm-Funktionen InputBox(), Msgbox() und Print(). Es gibt Python-Alternativen, die sich entweder auf das LibreOffice API Abstract Windowing Toolkit stützen oder auf Funktionsaufrufe von Python zu Basic. Letzteres schlägt eine Syntax vor, die der von Basic absichtlich nahe kommt und ein Python-Modul neben einem Basic-Modul verwendet. Das API Scripting Framework wird zum Ausführen von Funktionsaufrufen zwischen den Sprachen Basic, BeanShell, JavaScript und Python verwendet.

Python-Syntax:

MsgBox(txt, buttons=0, title=None)

InputBox(txt, title=None, default=None)

Print(txt)

Beispiele:

>>> import screen_io as ui

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

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

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

>>> ui.Print(age)

Installation:

Python-Modul screen_io


        # -*- coding: utf-8 -*-
        from __future__ import unicode_literals
        
        def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
            """ Einen Dialog mit einer Meldung anzeigen und einen Wert zurückgeben. """
            xScript = _getScript("_MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
            """ Eine Eingabeaufforderung in einem Dialog anzeigen, in die der Benutzer Text eingeben kann. """
            xScript = _getScript("_InputBox")
            res = xScript.invoke((prompt,title,defaultValue), (), ())
            return res[0]
        
        def Print(message: str):
            """ Die angegebenen Zeichenketten oder numerischen Ausdrücke in einem Dialog ausgeben. """
            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
    

Basic-Modul uiScripts


        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