Ask Your Question

Python macro to create new cell style

asked 2020-07-19 17:37:33 +0200

kazhuu gravatar image

updated 2020-07-27 13:37:33 +0200

I was programming this LibreOffice project: and I'm wondering how to create new cell style in Python using pyuno. So far I've followed this example: 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: 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?

edit retag flag offensive close merge delete


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.

Jim K gravatar imageJim K ( 2020-07-20 23:06:29 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2020-07-19 18:29:52 +0200

mauricio gravatar image

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)
edit flag offensive delete link more


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

kazhuu gravatar imagekazhuu ( 2020-07-19 19:58:12 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-07-19 17:37:33 +0200

Seen: 69 times

Last updated: Jul 27