Jump to content

Comment rechercher du texte figurant entre des balises ?

From The Document Foundation Wiki
This page is a translated version of the page Macros/Writer/007 and the translation is 64% complete.


Description

On souhaite retrouver par programme le texte figurant entre une balise de début et une balise de fin. Par exemple retrouver ce qui figure dans un document entre début d'explication et fin d'explication.

Le principe est de rechercher la "balise" de début, ce qui donne une position de départ pour rechercher la "balise" de fin. Ces deux positions obtenues on sélectionne via le curseur visible depuis la fin de la balise début jusqu'au début de la balise de fin.

La recherche se fait via la méthode createSearchDescriptor du com.sun.star.util.XSearchable[1] qui utilise le com.sun.star.util.SearchDescriptor[2] du par lequel on définit les propriétés de la recherche : texte recherché, respect de la casse, utilisation d'expressions régulières, etc.

Le service com.sun.star.text.XTextViewCursor[3] est utilisé pour afficher le texte localisé

Code

Dans cette macro, les "balises" de début et de fin sont "début d'explication" et "fin d'explication".

Option Explicit

Sub SearchText()
    '''Rechercher du texte figurant entre des balises'''

    Dim oDoc As Object ' com.sun.star.text.GenericTextDocument, com.sun.star.util.XSearchable
    Dim oSearchZone As Object ' com.sun.star.util.SearchDescriptor
    Dim oFoundZone As Object ' com.sun.star.uno.XInterface
    Dim oFoundAfterZone As Object ' com.sun.star.uno.XInterface
    Dim oCursVisible As Object ' com.sun.star.text.XTextViewCursor

    oDoc = ThisComponent ' com.sun.star.util.XSearchable compliant document
    oSearchZone = oDoc.createSearchDescriptor()
    With oSearchZone ' com.sun.star.util.SearchDescriptor
        .SearchString = "début d'explication"
    End With
    oFoundZone = oDoc.findFirst(oSearchZone)

    If Not(IsNull(oFoundZone)) Then
        With oSearchZone ' com.sun.star.util.SearchDescriptor
            .SearchString = "fin d'explication"
        End With
        oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
        If Not(IsNull(oFoundAfterZone)) Then
            oCursVisible = oDoc.currentcontroller.viewCursor
            With oCursVisible ' com.sun.star.text.text.XTextViewCursor
                .gotoRange(oFoundZone.End, false)
                .gotoRange(oFoundAfterZone.Start, True)
                MsgBox .String, MB_ICONINFORMATION, "Texte trouvé"
            End With
        Else
            MsgBox "balise de fin non trouvée", MB_ICONINFORMATION, "Recherche"
        End If
    Else
        MsgBox "balise de début non trouvée", MB_ICONINFORMATION, "Recherche"
    End If

End Sub ' SearchText

With Pythonː

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

MB_ICONINFORMATION = 64

def SearchText():
    """<span lang="en" dir="ltr" class="mw-content-ltr">Search 1 Highlight text found between two tags</span>
    
    oSearchZone         com.sun.star.util.SearchDescriptor
    oFound(After)Zone   com.sun.star.uno.XInterface
    oCursVisible        com.sun.star.text.XTextViewCursor
    """
    oDoc = XSCRIPTCONTEXT.getDocument()  # <span lang="en" dir="ltr" class="mw-content-ltr">com.sun.star.util.XSearchable compliant document</span>
    oSearchZone = oDoc.createSearchDescriptor()
    oSearchZone.SearchString = "<span lang="en" dir="ltr" class="mw-content-ltr">start of explanation</span>"
    oFoundZone = oDoc.findFirst(oSearchZone)
    if oFoundZone:
        oSearchZone.SearchString = '<span lang="en" dir="ltr" class="mw-content-ltr">end of explanation</span>'
        oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
        if oFoundAfterZone:
            oCursVisible = oDoc.CurrentController.ViewCursor
            oCursVisible.gotoRange(oFoundZone.End, False)
            oCursVisible.gotoRange(oFoundAfterZone.Start, True)
            MsgBox(oCursVisible.String,MB_ICONINFORMATION, "<span lang="en" dir="ltr" class="mw-content-ltr">Text was found</span>")
        else:
            MsgBox('<span lang="en" dir="ltr" class="mw-content-ltr">end tag was NOT found</span>',MB_ICONINFORMATION, "<span lang="en" dir="ltr" class="mw-content-ltr">Text search</span>")
    else:
        MsgBox("<span lang="en" dir="ltr" class="mw-content-ltr">start tag was NOT found</span>",MB_ICONINFORMATION, '<span lang="en" dir="ltr" class="mw-content-ltr">Text search</span>')

def MsgBox(prompt="", buttons=MB_ICONINFORMATION, title=""):  # <span lang="en" dir="ltr" class="mw-content-ltr">Text search</span>
    pass

Python standard output file is not available when running Python macros. Input/Output to Screen help page exposes alternatives.

Fichier ODT pour tester la macro

Notes