Conteúdo
Índice 🔎︎

# 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.

A extensão Alternative Python Script Organizer (APSO) oferece a função msgbox() dentro do módulo apso_utils.

O LibreOffice Basic oferece as funções de E/S de ecrã InputBox(), Msgbox() e Print(). As alternativas Python existem a partir tanto do LibreOffice API Abstract Windowing Toolkit, quanto pelas chamadas para o BASIC do Python. Esta última oferece uma sintaxe intencionalmente similar ao BASIC, e utiliza um módulo Python junto com um módulo BASIC. Utiliza-se o API Scripting Framework para executar chamadas de função entre BASIC, BeanShell, JavaScript e Python.

## Sintaxe Python:

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

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

Print(txt)

## Exemplos:

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

## Instalação:

- Copy screen_io Python module in My macros within <UserProfile>/Scripts/python/pythonpath,

- Copy uiScripts Basic module in My macros Standard Basic library,

- Reinicie o LibreOffice

### Módulo Python screen_io

            # -*- coding: utf-8 -*-
            from __future__ import unicode_literals

            def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
                """ Exibe uma caixa de diálogo contendo uma mensagem e retorna um 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

### Módulo Basic 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

Tópicos relacionados

Funções de E/S do ecrã

Exemplos de programação em Python

Ajuda para os scripts Python no LibreOffice

Calling Basic Macros from Python