Search code examples
pythonsvgsvgwrite

TextPath with svgwrite - ValueError: Invalid children 'textPath' for svg-element <svg>


I have problems to use TextPath element with svgwrite library. I followed the documentation https://pythonhosted.org/svgwrite/classes/text.html#textpath

I have path:

w = dwg.path(d="M150 150 L2000 2000 L150 2000 Z", stroke="green")

And that path I use as a path for a text:

dwg.add(svgwrite.text.TextPath(path=w, text="blab blab bla bal", startOffset=None, method='align', spacing='exact'))

When I try that, I obtain error:

ValueError: Invalid children 'textPath' for svg-element <svg>

If I just create the TextPath element and do not add it to dwg, it throws no error.

What I am missing? Thanks for any advice.


Solution

  • The parent of a textPath element must be a text element. In your case you're adding the textPath as a child of your root svg element which is not valid.

    text = dwg.add(svgwrite.text.Text(""))
    text.add(svgwrite.text.TextPath(path=w, text="blab blab bla bal", startOffset=None, method='align', spacing='exact'))