Macro Calc: In page style shows attribute error (<class 'AttributeError'>: FirstIsShared

In Page Style I want to activate the option: ‘Same content on first page’ , through a python macro.
But this error is showing:
(<class ‘AttributeError’>: FirstIsShared

what am I doing wrong? The same approach works for the other attributes.

custom_pg_style.FirstIsShared = True

My macro:

import uno

def test_header(*args):
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets
    main_sheet = sheets[0]


    #Create new Page Style
    pg_styles = doc.StyleFamilies['PageStyles']
    if pg_styles.hasByName('Custom') == False:
        page_style = doc.createInstance('com.sun.star.style.PageStyle')
        pg_styles.insertByName('Custom', page_style)

    #Apply Page Style
    main_sheet.PageStyle = 'Custom'

    #Modify Page Style
    custom_pg_style = pg_styles[main_sheet.PageStyle]
    #custom_pg_style.HeaderOn = True #it works
    #custom_pg_style.HeaderIsShared = True #it works

    #https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1style_1_1PageProperties.html#a7381f863bce3a1e7a0ba3838a2636364
    custom_pg_style.FirstIsShared = True

https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1style_1_1PageProperties.html#a7381f863bce3a1e7a0ba3838a2636364

I am thinking this should be working.

In OooDev (python) I did exactly what you are trying to accomplish here. The FirstIsShared is the correct property.

You can see in the Help Docs that it is working.

I think custom_pg_style should implement com.sun.star.beans.XPropertySet.
I found setting properties in the API buggy. See my Prop.get() methon in OooDev that show some of the complexites of this.

Have you tired custom_pg_style.setPropertyValue("FirstIsShared", True)

1 Like

Sorry, I was not paying close enough attention and was thinking about Writer and not Calc.

In OooDev the Help for Calc Set Header is here.

Many of the Propertes for header and footer are named diferently.

You can see the header property in Calc Header file.

Footer property names here.

Your looking for custom_pg_style.FirstPageHeaderIsShared = True.

I tested using OooDev, you can see the source on pastebin.

1 Like

That was the error, the name of header attributes changes between Writer and Calc.

Now the code worked for me.
Thanks

1 Like