[Solved] [Python][Writer] Page numbering in footer - Style

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
yijinger
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

[Solved] [Python][Writer] Page numbering in footer - Style

Post by yijinger »

Hi all,

another problem with not evident solution: insert page numbers into a new document's footer. After reading lots of manuals and (fortunately) some forum code sheets, I came to the following:

Code: Select all

from com.sun.star.text.PageNumberType import PREV as PAGING_PREV, CURRENT as PAGING_CURRENT, NEXT as PAGING_NEXT
from com.sun.star.style.NumberingType import ARABIC
from com.sun.star.style.ParagraphAdjust import RIGHT as PARA_ADJUST_RIGHT, LEFT as PARA_ADJUST_LEFT

...

        addpages_doc = self.desktop.loadComponentFromURL(not_paged_doc_url, "_default", 0, ())

        addpages_doc.StyleFamilies.PageStyles.Standard.FooterIsOn = True      # after setting it to True "FooterText" appears magically in addpages_doc.StyleFamilies.PagesStyles.Standard
        addpages_doc.StyleFamilies.PageStyles.Standard.FooterHeight = 1000
        addpages_doc.StyleFamilies.PageStyles.Standard.FirstIsShared = False   # to remove numbering from the title page

        # the very page number we'll use
        page_numbering = addpages_doc.createInstance("com.sun.star.text.textfield.PageNumber")
        page_numbering.NumberingType = ARABIC
        page_numbering.Offset = 0
        page_numbering.SubType = PAGING_CURRENT

        # was not easy to find
        footer = addpages_doc.StyleFamilies.PageStyles.Standard.FooterText
        footer_cursor = footer.createTextCursor()
        footer_cursor.gotoEnd(False)
        footer_cursor.ParaAdjust = PARA_ADJUST_RIGHT
        # footer_cursor.CharHeight = 10    # commented properties do not work somewhy
        # footer_cursor.CharFontName = 'verdana'
        # footer_cursor.CharColor = 0x7f0000   # dark red
        # footer_cursor.CharPosture = ITALIC
        # footer_cursor.CharWeight = FW_BOLD
        footer.insertTextContent(footer_cursor, page_numbering, False)

        save_arg = PropertyValue(Name="Overwrite", Value=True)
        addpages_doc.storeToURL(paged_doc_url, (save_arg,))
Almost everything is good, but I cannot control text fully. When I try to set CharColor or CharHeight, I cannot do it, and after "insertTextContent()" call they are reset to "-1" and "12.0" respectively. I tried to find other ways to change font style, but in vain. Could you please tell me, how I can set red color and italic posture for page numbers?

The posts I read before creating my code are: viewtopic.php?f=44&t=71155 and viewtopic.php?f=44&t=89998 .
Last edited by yijinger on Tue Apr 06, 2021 9:00 pm, edited 1 time in total.
LibreOffice 6.0.7.3 on Linux Mint 20
yijinger
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

Re: [Python][Writer] Page numbering in footer - font style

Post by yijinger »

Well, I played with formattings and cursors and finally solved this puzzle. Maybe not very neat solution, but it works. I'll show the whole code (with already known part).

Code: Select all

        addpages_doc = self.desktop.loadComponentFromURL(not_paged_doc_url, "_default", 0, ())

        addpages_doc.StyleFamilies.PageStyles.Standard.FooterIsOn = True
        addpages_doc.StyleFamilies.PageStyles.Standard.FooterHeight = 1000
        addpages_doc.StyleFamilies.PageStyles.Standard.FirstIsShared = False

        page_numbering = addpages_doc.createInstance("com.sun.star.text.textfield.PageNumber")
        page_numbering.NumberingType = ARABIC
        page_numbering.Offset = 0
        page_numbering.SubType = PAGING_CURRENT

        footer = addpages_doc.StyleFamilies.PageStyles.Standard.FooterText
        footer_cursor = footer.createTextCursorByRange(footer.getEnd())
        footer_cursor.gotoEnd(False)
        footer_cursor.ParaAdjust = PARA_ADJUST_RIGHT
        footer.insertTextContent(footer_cursor, page_numbering, False)

        footer_cursor.TextParagraph.CharHeight = 14
        footer_cursor.TextParagraph.CharFontName = 'Liberation Sans'
        footer_cursor.TextParagraph.CharColor = 0xae9a
        footer.insertString(footer_cursor, "", False)

        save_arg = PropertyValue(Name="Overwrite", Value=True)
        addpages_doc.storeToURL(paged_doc_url, (save_arg,))
As you can see, the trick was in styling the whole paragraph (TextParagraph property) and adding an empty string after PageNumber field.
LibreOffice 6.0.7.3 on Linux Mint 20
Post Reply