Comment changer la taille ou la police de toutes les formules d'un document ?

    From The Document Foundation Wiki
    < Macros‎ | Math
    This page is a translated version of the page Macros/Math/001 and the translation is 20% complete.
    Outdated translations are marked like this.


    Other languages:

    Description

    Lorsque l'on veut modifier un attribut à toutes les formules que l'on va écrire, il suffit de suivre la procédure décrite dans la FAQ Math 008. Si l'on veut changer après coup, il faut réouvrir toutes les formules et faire les modifications. Si le nombre de formules est important, cela peut devenir vite fastidieux.

    Sur l'issue i#5092, il est proposé une macro que l'on peut adapter ainsi :

    Code

    En LibreOffice Basic:

    Option Explicit
    
    Sub ChangeFormulasSizeAndFont()
        '''Changer la taille ou la police de toutes les formules d'un document'''
        Dim embeddedObjects As Object ' com.sun.star.text.TextEmbeddedObjects
        Dim elementNames() As String ' Name array
        Dim element As Object ' com.sun.star.text.TextEmbeddedObject.Model
        Dim realElem As Object ' com.sun.star.text.TextEmbeddedObject.(get)EmbeddedObject
    
        embeddedObjects = ThisComponent.getEmbeddedObjects() ' com.sun.star.text.TextEmbeddedObjects
        elementNames = embeddedObjects.getElementNames() ' name array
    
        For i = 0 To UBound(elementNames)
    
            element = embeddedObjects.getByName(elementNames(i)).Model
            If (not isNull(element)) Then
                With element ' com.sun.star.text.TextEmbeddedObject.Model
                If .supportsService("com.sun.star.formula.FormulaProperties") Then
                    <span lang="en" dir="ltr" class="mw-content-ltr">' Force the update</span>
                    realElem = embeddedObjects.getByName(elementNames(i)).EmbeddedObject
                    realElem.updateMode = com.sun.star.embed.EmbedUpdateModes.ALWAYS_UPDATE
                    .BaseFontHeight = 14
                    .FontNameVariables = "Arial"
                    .FontNameFunctions = "Arial"
                    .FontNameNumbers = "Arial"
                    .FontNameText = "Arial"
                End If
                End With ' element As com.sun.star.formula.FormulaProperties
           End If
    
         Next i
     
        ThisComponent.reformat()
    
    End Sub ' ChangeFormulasSizeAndFont

    With Python:

    # coding: utf-8
    from __future__ import unicode_literals
    
    import uno
    # from com.sun.star.document import OfficeDocument
    
    FRAME_DESKTOP = 'com.sun.star.frame.Desktop'
    FORMULA_PROPERTIES = 'com.sun.star.formula.FormulaProperties'
    
    class UIFormulaCollection():
        <span lang="en" dir="ltr" class="mw-content-ltr">""" Current document formulae collection """</span>
        def __init__(self):
            <span lang="en" dir="ltr" class="mw-content-ltr">""" Grab current doc in UI by default """</span>
            # ctx = uno.getComponentContext()  # GetDefaultContext
            # smgr = ctx.getServiceManager()  # GetProcessServiceManager
            # desktop =  smgr.createInstanceWithContext(FRAME_DESKTOP, ctx)  # StarDesktop
            # self.doc = desktop.CurrentComponent  # ThisComponent
            self.doc = XSCRIPTCONTEXT.getDocument()
            self.objects = self.doc.EmbeddedObjects
            self.names = self.objects.getElementNames()
        def reformat(self, fontHeight=None, font=None):
            <span lang="en" dir="ltr" class="mw-content-ltr">""" add as many properties as desired """</span>
            for name in self.names:
                element = self.objects.getByName(name).Model
                if element.supportsService(FORMULA_PROPERTIES):
                    if font:
                        element.FontNameFunctions = font
                        element.FontNameText = font
                        element.FontNameNumbers = font
                        element.FontNameVariables = font
                    if fontHeight:
                        element.BaseFontHeight = fontHeight
                self.doc.reformat()
    
    
    def reformatAllFormulas():
        ui = UIFormulaCollection()
        ui.reformat(font="Arial", fontHeight=14)
    
    g_exportedScripts = reformatAllFormulas,


    ODT files to test macro

    Pick up any LibreOffice published Math guide for testing purpose.

    Notes

    • Consult API documentation about FormulaProperties to explore other properties that can be altered.