Je voudrais repérer les positions et les tailles de police de chacun des symboles utilisés dans un SVG d'une équation mathématique.
Je joue avec la bibliothèque d'analyse XML de Python : xml.etree.ElementTree ( https://docs.python.org/3/library/xml.etree.elementtree.html ).
Voici l'exemple de SVG que j'utilise :
example_svg = '''<svg style="vertical-align:-10.2252022445128pt" xmlns="http://www.w3.org/2000/svg" width="193pt" height="31pt" viewBox="-1 -1 193 31">
<path d="M43.875 16.305h20.426" fill="none" stroke-width=".914" stroke="#000" stroke-miterlimit="10"></path>
<g font-family="MathFont" font-size="13.5">
<text y="11.168" x="45.874">3</text>
<text y="11.168" x="52.532"></text>
<text y="28.382" x="50.758">4</text>
</g>
<g font-family="MathFont" font-size="9.45">
<text y="6.327" x="60.453">3</text></g>
</svg>'''
En Latex, l'équation est $ \frac {3x^3}{4}$.
L'utilisation du code suivant me donne presque tout ce que je veux, mais je ne parviens pas à le relier aux attributs du texte du groupe. Idéalement, je voudrais que la sortie soit (symbole, coordonnée_y, coordonnée_x, famille de caractères, taille de caractères).
import xml.etree.ElementTree as ET
root = ET.fromstring(example_svg)
for tag in root.findall('.//{http://www.w3.org/2000/svg}text'):
symbol = tag.text
y_coord = tag.get('y')
x_coord = tag.get('x')
print(symbol, y_coord, x_coord)