From b7d16102eb27cdd2d8b80a5815dd0d2599b19094 Mon Sep 17 00:00:00 2001 From: a2nr Date: Mon, 20 Feb 2023 07:51:46 +0700 Subject: [PATCH] init --- coba.py | 21 +++++++++++++++++++++ screen_io.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 coba.py create mode 100644 screen_io.py diff --git a/coba.py b/coba.py new file mode 100644 index 0000000..fe6ea6a --- /dev/null +++ b/coba.py @@ -0,0 +1,21 @@ +import sys +def PythonVersion(*args): + """Prints the Python version into the current document""" +#get the doc from the scripting context which is made available to all scripts + desktop = XSCRIPTCONTEXT.getDesktop() + model = desktop.getCurrentComponent() +#check whether there's already an opened document. Otherwise, create a new one + if not hasattr(model, "Sheets"): + model = desktop.loadComponentFromURL( + "private:factory/scalc","_blank", 0, () ) +#get the XText interface + sheet = model.Sheets.getByIndex(0) +#create an XTextRange at the end of the document + tRange = sheet.getCellRangeByName("C4") +#and set the string + tRange.String = "The Python version is %s.%s.%s" % sys.version_info[:3] +#do the same for the python executable path + tRange = sheet.getCellRangeByName("C5") + tRange.String = sys.executable + return None + diff --git a/screen_io.py b/screen_io.py new file mode 100644 index 0000000..9b22556 --- /dev/null +++ b/screen_io.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int: + """ Displays a dialog 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='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