Calling a libreoffice basic function from a python script

I started experimenting with calling a basic function from a python script. I was surprised that without to much trouble I got it to work to a certain extent.

I am working on linux Mint.

I started with a basic Sub I found. In it's original form it had 3 rgb values hard coded and it popped up a message box that displayed the color value from the rgb input. I rewrote it as a Function that took the rgb values as parameters.

Here is the basic:

Function ColorExampleFunc(r as long, g as long, b as long)
Dim lVar As Long
lVar = rgb(207,231,245)
msgbox "The color " & lVar & " consists of:" & Chr(13) &_
"red= " & red(lVar) & Chr(13)&_
"green= " & green(lVar) & Chr(13)&_
"blue= " & blue(lVar) & Chr(13) , 64,"colors"

end function

Here is the python:

#test_basic.py
import os

result = os.system('soffice "macro:///Standard.Module1.ColorExampleFunc(207,231,245)"')

This works as it will pop up the message box with the color value displayed. What I need to do is capture that color value (which is usually an eight digit number) in variable in the python script.

I printed result and it was equal to 0.

It's been a long time since I did anything in basic. I imagine I would have to get rid of the msgbox code and set the rgb values equal to a variable and then return them, but I'm not sure how to do it.

Does anyone know how to get the color value in the python script?

Thanks, Jim

This page shows the answer to your question:

https://stackoverflow.com/questions/2781689/how-to-return-a-result-from-a-vba-function

I started experimenting with calling a basic function from a python
script. I was surprised that without to much trouble I got it to work to
a certain extent.

I am working on linux Mint.

I started with a basic Sub I found. In it's original form it had 3 rgb
values hard coded and it popped up a message box that displayed the
color value from the rgb input. I rewrote it as a Function that took the
rgb values as parameters.

Here is the basic:

Function ColorExampleFunc(r as long, g as long, b as long)
Dim lVar As Long
lVar = rgb(207,231,245)
msgbox "The color " & lVar & " consists of:" & Chr(13) &_
"red= " & red(lVar) & Chr(13)&_
"green= " & green(lVar) & Chr(13)&_
"blue= " & blue(lVar) & Chr(13) , 64,"colors"

end function

Here is the python:

#test_basic.py
import os

result = os.system('soffice
"macro:///Standard.Module1.ColorExampleFunc(207,231,245)"')

This works as it will pop up the message box with the color value
displayed. What I need to do is capture that color value (which is
usually an eight digit number) in variable in the python script.

I printed result and it was equal to 0.

Well, you "rewrote it as a function", except that you didn't. The
difference between subroutines and functions (at least in Basic), is that
functions returns something. Yours don't, as far as I can see.
To return a value in Basic (at least LibreOffice Basic), you just set a
value to the function itself:

ColorExampleFunc=Something

I'm not sure what those eight digit numbers you are looking for are. Is it
something like b+g×2⁸+r×2¹⁶?

Kind regards

Johnny Rosenberg

It is the result of lVar = rgb(207,231,245), which in this case is 13625333 and that is the color value of blue_classic.

So if I rewrote the function as:

Function ColorExampleFunc(r as long, g as long, b as long)
Dim lVar As Long
lVar = rgb(207,231,245)
ColorExampleFunc = lVar
end function

and called it from libreoffice basic it would return 13625333? I ask because when I call it from my python script it returns 0. If I know it is working in basic then I can concentrate on figuring out what is happening on the python part.

Thanks, Jim

This page shows the answer to your question:

https://stackoverflow.com/questions/2781689/how-to-return-a-result-from-a-vba-function

OK, thanks

Jim

You need used MasterScriptProviderFactory

Change the original Basic function to:

Function ColorExampleFunc(r As Integer, g As Integer, b As Integer) As
Long
  ColorExampleFunc = RGB(r, g, b)
end function

Then you can call, look my example:
https://gitlab.com/snippets/1977700

Best regards

Mauricio,

Thanks for sharing your script with me. I ran it but got a Segmentation fault. I scattered some print()'s throughout the script and it looks like this is the line that may be causing the problem.

script = factory.createScriptProvider('').getScript(url) which is in the
def _call_macro_basic(location, args) function. Execution does not continue past that line.

I have been working on automating some of my worksheets using OOSheet. This means that I must start libreoffice calc like this:

libreoffice /home/jfb/Dev/Test_OOSheet.ods --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

Maybe this is why the script works for you and gives me the Segmentation fault. I am going to keep trying get it to work for me as I think it offers me the best chance of doing what I want to do.

Thanks, Jim

The script work fine, if you execute into LibreOffice. If you want
execute outside LibreOffice, you need used other technique.

But, if you tell me "exactly" your context and that you "exactly" want,
I can help you, but I don't like to guess.

I am enhancing a python script that changes background colors based on hard coded color values. I would like to be able to enter the color I want to use as rgb and receive the color value back to be used in the color changing part of the script.

I was googling for ways to use rgb values to get back a color value when I came across the libreoffice basic function that did what I wanted. That is the only reason I was looking at calling a basic function and getting a return value. I would much prefer a python solution but could not find one.

Thanks, Jim

color = (r << 16) + (g << 8) + b

As a hobbyist programmer it took me a while to figure out what you were doing with that line of code. But I finally did and it does exactly what I need.

Thank you very much.

Jim