I would like to pick out the positions and font sizes of each of the symbols used in an SVG of a mathematical equation.
I am playing around with the Python XML parsing library: xml.etree.ElementTree (https://docs.python.org/3/library/xml.etree.elementtree.html).
Here is the example SVG I am using:
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>'''
In Latex the equation is $\frac{3x^3}{4}$.
Using the following code gives me nearly everything I want, but I can’t seem to connect this to the attributes in group text. Ideally I want the output to be (symbol, y_coord, x_coord, font-family, font-size).
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)