[Solved] Resources for Python, Where to look ?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
umfan110
Posts: 3
Joined: Wed Sep 09, 2020 9:15 pm

[Solved] Resources for Python, Where to look ?

Post by umfan110 »

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.
OpenOffice 3.1 Windows 10
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Resources for Python, Where to look ?

Post by Villeroy »

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
FJCC
Moderator
Posts: 9619
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Resources for Python, Where to look ?

Post by FJCC »

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 Sub
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.
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Resources for Python, Where to look ?

Post by Villeroy »

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
umfan110
Posts: 3
Joined: Wed Sep 09, 2020 9:15 pm

Re: Resources for Python, Where to look ?

Post by umfan110 »

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() 
    
    return
OpenOffice 3.1 Windows 10
FJCC
Moderator
Posts: 9619
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Resources for Python, Where to look ?

Post by FJCC »

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.
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Resources for Python, Where to look ?

Post by Villeroy »

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.
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
umfan110
Posts: 3
Joined: Wed Sep 09, 2020 9:15 pm

Re: Resources for Python, Where to look ?

Post by umfan110 »

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
Post Reply