We will be migrating from Ask to Discourse on the first week of August, read the details here

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: https://github.com/Kazhuu/movelister and I'm wondering how to create new cell style in Python using pyuno. So far I've followed this example: https://wiki.openoffice.org/wiki/Docu.... From that example cell style is made with call:

Object aCellStyle = xServiceManager.createInstance("com.sun.star.style.CellStyle");

Which is then inserted with name to the style family. Now because of Python I've followed this guide to write macros in python: https://wiki.openoffice.org/wiki/Pyth.... It says I should write following:

smgr.createInstanceWithContext("com.sun.star.style.CellStyle", ctx) or smgr.createInstance("com.sun.star.style.CellStyle")

But the problem is that both calls return None for me. I also have tried using: uno.createUnoStruct('com.sun.star.sheet.CellStyle') but this one raises exception: uno.com.sun.star.uno.RuntimeException: pyuno.getClass: uno exception com.sun.star.sheet.TableCellStyle 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

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

doc = XSCRIPTCONTEXT.getDocument()

name = 'MyStyle'
new_style = doc.createInstance('com.sun.star.style.CellStyle')

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: 231 times

Last updated: Jul 27 '20