[Solved] Resources for Python, Where to look ?
[Solved] Resources for Python, Where to look ?
Hows it going guys. I would like to start making little scripts in python for Openoffice / Libreoffice. Anyone know where I can find an API? I see in the examples com.sun.star.text and what not but when I follow that It takes me to the API for some other language (Basic maybe?). I cant seem to find a clear translation between that API and python. I also looked at UNO tools for python but it leads me back here to the openoffice general pages. If anyone could point me in the right direction that be awesome !
Last edited by Hagar Delest on Thu Sep 10, 2020 5:08 pm, edited 1 time in total.
Reason: tagged solved.
Reason: tagged solved.
OpenOffice 3.1 Windows 10
Re: Resources for Python, Where to look ?
https://api.libreoffice.org/docs/idl/re ... 1star.html
viewtopic.php?f=74&t=49294
http://www.openoffice.org/udk/python/python-bridge.html
viewtopic.php?f=74&t=49294
http://www.openoffice.org/udk/python/python-bridge.html
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Resources for Python, Where to look ?
I was typing this up when Villeroy posted, so I'll put it up as an addition to his answer. Coding in Basic and Python is much the same. The most important difference is probably that Python is case sensitive and Basic is not, so you have to be careful when transferring code from Basic to Python. Below are two versions of a very simple macro, first in Python then in Basic. The differences are as much due to the languages as the API.
Code: Select all
def setArry():
doc = XSCRIPTCONTEXT.getDocument()
sheets = doc.Sheets
sheet1 = sheets.getByName('Sheet1')
oRng = sheet1.getCellRangeByName("A1:C3")
DArray = (("a", "B", "C"), ("D", "", "F"), ("G", "H", "I"))
oRng.setDataArray(DArray)Code: Select all
Sub Main
doc = ThisComponent
sheets = doc.Sheets
sheet1 = sheets.getByName("Sheet1")
oRng = sheet1.getCellRangeByName("A1:C3")
DArray = Array(Array("a", "B", "C"), Array("D", "", "F",), Array("G", "H", "I"))
oRng.setDataArray(DArray)
End SubOpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Resources for Python, Where to look ?
Indeed, if the macros are written properly and clearly you can translate hundreds of code lines easily by simple copy/paste and then walk through line by line fixing details until there is no more compilation error. Of course you can write more elegant and more efficient code in Python when it comes to iterations, string manipulations and system related stuff but the calls to the UNO API are the same.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Resources for Python, Where to look ?
Thanks for the reply guys ! I see you are right they are quite similar. So Villeroy linked the SDK API reference. When I try to follow the commented section within the linked API I dont see getText(). Am I missing something? Is there another page with more classes. Or do they relate within the getDocument() class ?
Code: Select all
import uno
def my_first_macro_writer():
doc = XSCRIPTCONTEXT.getDocument()
text = doc.getText() # com.sun.star.text.Text
text.setString('Hello World in Python in Writer')
pr = XPrintable.print()
returnOpenOffice 3.1 Windows 10
Re: Resources for Python, Where to look ?
Learning to write macros using the API documentation will be very difficult. I strongly suggest you use the MRI extension that is explained in one of the links Villeroy provided. You can then easily see the properties and methods available for any object. Once it is installed, you can look in the OpenOffice Help under MRI Documentation -> How to Run for how to use it in Python.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Resources for Python, Where to look ?
That example macro operates with a text document which supports service com.sun.star.text.TextDocument providing interface XTextDocument with function getText() returning the whole text body (? as far as I know. don't know much about Writer).
Macro programming without the MRI extension is pointless. You need it. Install it. Play with it.
An "UNO service" is basically an object once it has been instanciated.
An UNO interface is a bundle of one or more methods (functions). If some service supports c.s.s.container.XNameAccess you can be sure that it supports all 3 methods getByName(s), hasByName(s), getElementNames() as documented.
So you have objects with properties and methods just like with VBA but the logic and the terminology is different because the entire structure is language independent.
Macro programming without the MRI extension is pointless. You need it. Install it. Play with it.
An "UNO service" is basically an object once it has been instanciated.
An UNO interface is a bundle of one or more methods (functions). If some service supports c.s.s.container.XNameAccess you can be sure that it supports all 3 methods getByName(s), hasByName(s), getElementNames() as documented.
So you have objects with properties and methods just like with VBA but the logic and the terminology is different because the entire structure is language independent.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Resources for Python, Where to look ?
Thanks for the feedback guys. I was messing around with it last night and I think I am getting the hang of it. I saw the MRI documentation and am looking at it more closely. Thanks again for the support and clarification!
OpenOffice 3.1 Windows 10