Python macro to create new cell style

I was programming this LibreOffice project: GitHub - Kazhuu/movelister: LibreOffice Calc based program with Python macros for generating tables and sheets for detailed video game character mechanics notes and I’m wondering how to create new cell style in Python using pyuno. So far I’ve followed this example: Overall Document Features - Apache OpenOffice Wiki. From that example cell style is made with call:

Object aCellStyle = xServiceManager.createInstance(“”);

Which is then inserted with name to the style family. Now because of Python I’ve followed this guide to write macros in python: Transfer from Basic to Python - Apache OpenOffice Wiki. It says I should write following:

smgr.createInstanceWithContext(“”, ctx) or smgr.createInstance(“”)

But the problem is that both calls return None for me. I also have tried using:
uno.createUnoStruct(‘’) but this one raises exception: pyuno.getClass: uno exception is unknown

Is creating styles supported by Python macros and pyuno or am I missing something?

In the Java example, the xServiceManager interface is obtained from xDocument. Since Python does not care about the type, there is no need to specify which interface is required, so we can simply use the document object.

You need create instance from document, you see this example:

doc = XSCRIPTCONTEXT.getDocument()

name = 'MyStyle'
new_style = doc.createInstance('')

cell_styles = doc.getStyleFamilies()['CellStyles']

if not cell_styles.hasByName(name):
    cell_styles.insertByName(name, new_style)

    properties = dict(
        CharFontName = 'Liberation Sans',
        CharHeight = 20.0,
        CharWeight = 150,
        CharPosture = 2,
        VertJustify = 2,
        HoriJustify = 2,
    keys = tuple(properties.keys())
    values = tuple(properties.values())
    new_style.setPropertyValues(keys, values)

Thanks man! So simple to do in the end :slight_smile: