Entrada y salida de y hacia la pantalla

La salida estándar de Python no está disponible cuando se ejecutan las macros Python desde el menú Herramientas ▸ Macros ▸ Ejecutar macro. Presentar la salida de un módulo requiere la consola interactiva de Python. Las funciones como input(), print(), repr() y str() están disponibles desde la consola de Python.

tip

LibreOffice msgbox Python module proposes a msgbox() method that is illustrated in Creating Event Listeners and Creating a dialog handler example pages.


LibreOffice Basic propone las funciones de E/S de pantalla InputBox(), Msgbox() y Print(). Existen alternativas de Python que se basan en el kit de herramientas de ventanas abstractas de la API de LibreOffice, o bien en las llamadas de funciones de Python a Basic. Esta última alternativa propone una sintaxis que es intencionalmente cercana a la de Basic y utiliza un módulo de Python junto con un módulo de Basic. El marco de programación de macros de la API se utiliza para realizar llamadas de funciones entre los lenguajes Basic, BeanShell, JavaScript y Python.

Sintaxis de Python:

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

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

Print(txt)

Ejemplos:

>>> 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)

Instalación:

Módulo screen_io de Python


        # -*- coding: utf-8 -*-
        from __future__ import unicode_literals
        
        def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
            """ Muestra un cuadro de diálogo que contiene un mensaje y devuelve un valor."""
            xScript = _getScript("_MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
            """ Displays a prompt in a dialog 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 dialog 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
    
note

MsgBox and InputBox methods from the Basic service included in the ScriptForge libraries call directly their native Basic counterparts.


Módulo uiScripts de Basic


        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
    
tip

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


¡Necesitamos su ayuda!