How to change header text via macro in python in Calc?

Based on this solution in basic:

I tried to do something similar in python but it doesn’t work

doc = XSCRIPTCONTEXT.getDocument()
sheets = doc.Sheets
my_sheet = sheets['mySheet1']

my_header = my_sheet.PageSetup.CenterHeader
new_header = my_header.replace(' - ', ' - test - ')
my_sheet.PageSetup.CenterHeader = new_header

print(new_header)

In Calc you need edit the style page.

    doc = XSCRIPTCONTEXT.getDocument()
    my_sheet = doc.Sheets['Sheet1']
    print(my_sheet.PageStyle)

    page_style = doc.StyleFamilies['PageStyles'][my_sheet.PageStyle]

    header = page_style.RightPageHeaderContent
    text = header.CenterText
    text.String = "Center"
    page_style.RightPageHeaderContent = header
1 Like

Thank you for your help.
And how to use the formatting codes for the header similar to vba ?

so it didn’t work

text.String = "Test: &A - &D"

No idea, I haven’t used VBA for many years.

but, the code works, as you can see. I don’t know how you are implementing it. Without seeing what you are doing, it is difficult to help you.

text.String = "Test: &A - &D"

code: &A : is the name of the sheet
code: &D : is the current date

I wanted to create something like the image below. Where sheet name and date are as a custom field

not just as a string “Test: &A - &D”

At the bottom of this page is what you are looking for.

https://wiki.openoffice.org/wiki/ES/Manuales/GuiaAOO/TemasAvanzados/Macros/StarBasic/TrabajandoConCalc/DandoFormato/FormatoPagina

1 Like

for future inquiries from other users:

footer Writer options:
https://forum.openoffice.org/en/forum/viewtopic.php?t=104958

Header Calc options:

from com.sun.star.awt import FontWeight

def set_header_ctt(header_content):
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets

    #more options: https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields
    field_sn = doc.createInstance("com.sun.star.text.TextField.SheetName")
    field_date = doc.createInstance("com.sun.star.text.TextField.DateTime")
    field_date.IsDate = True
    
    header_content.CenterText.String = ''

    oCursor = header_content.CenterText.createTextCursor()

    oCursor.CharHeight = 9  #change font size
    
    # oCursor.CharFontName = 'verdana'
    # oCursor.CharWeight = FontWeight.BOLD
    # oCursor.CharHeight = 15
    # oCursor.CharColor = 0x7f0000   # dark red
    # oCursor.CharPosture = 'ITALIC'

    """ print(oCursor.CharFontName)
    print(oCursor.CharWeight)
    print(oCursor.CharHeight)
    print(oCursor.CharPosture) """

    oCursor.gotoEnd(False)
    oCursor.String = f'''Custom Header: '''
    oCursor.gotoEnd(False)
    header_content.CenterText.insertTextContent (oCursor, field_sn, True)
    oCursor.gotoEnd(False)
    oCursor.String = ' - ['
    oCursor.gotoEnd(False)
    header_content.CenterText.insertTextContent (oCursor, field_date, True)
    oCursor.gotoEnd(False)
    oCursor.String = ']'

    return header_content


def main(*args):
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets
    oSheet = sheets['mySheet1']

    page_style = doc.StyleFamilies['PageStyles'][oSheet.PageStyle]
    first_header = page_style.FirstPageHeaderContent
    rest_header = page_style.RightPageHeaderContent

    new_first_header = set_header_ctt(first_header)
    page_style.FirstPageHeaderContent = new_first_header

    new_rests_header = set_header_ctt(rest_header)
    page_style.RightPageHeaderContent = new_rests_header

    return
1 Like