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

From The Document Foundation Wiki
< Macros‎ | Math
Jump to navigation Jump to search
This page is a translated version of the page Macros/Math/001 and the translation is 33% complete.
Outdated translations are marked like this.
Other languages:
English • ‎français

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
                ' Force the update
                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():
    """ Current document formulae collection """
    def __init__(self):
        """ Grab current doc in UI by default """
        # 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):
        """ add as many properties as desired """
        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.