python - 使用 SVGWRITE 在 Python 中添加指向 SVG 元素的链接

标签 python svg svgwrite

我正在尝试构建一个具有线条特征的 SVG,每条线条都会链接到同一文档中其他位置的部分。我不断收到 ValueError但是,指出“Invalid children 'line' for svg-element <a>.

此 MWE 重现错误:

import svgwrite

test = svgwrite.Drawing('test.svg', profile='tiny',size=(100, 100))

link = test.add(test.a('http://stackoverflow.com'))
link.add(test.line(start=(0,0),end=(100,100)))

test.save()

我对其他绘图元素(省略号、矩形等)也遇到同样的错误,但这些元素肯定可以作为链接的子元素。

我错过了什么?

Python版本:2.7.10 svgwrite版本:1.1.6(pkg_resources报告)

最佳答案

您似乎在 svgwrite 中使用了完全错误的元素创建语法。

根据文档链接是通过调用 svgwrite.container.Hyperlink 创建的和线路通过调用 svgwrite.shapes.Line

修复此问题后,您仍然看不到任何内容,因为没有笔划集的线条是不可见的。我添加了一个描边,并将描边宽度设置为比下面的正常宽度宽,这样就可以单击一些内容。

import svgwrite

test = svgwrite.Drawing('test.svg', profile='tiny',size=(100, 100))

link = test.add(svgwrite.container.Hyperlink('http://stackoverflow.com'))
link.add(svgwrite.shapes.Line(start=(0,0),end=(100,100),stroke_width="5",stroke="black"))

test.save()

这会产生以下输出

<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="tiny" height="100" version="1.2" width="100" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"><defs /><a target="_blank" xlink:href="http://stackoverflow.com"><line stroke="black" stroke-width="5" x1="0" x2="100" y1="0" y2="100" /></a></svg>

关于python - 使用 SVGWRITE 在 Python 中添加指向 SVG 元素的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48842782/

相关文章:

python - 如何在 svgwrite 中实现文本换行?

python - 使代码不那么复杂,更具可读性

python - 从大型数据框中删除 pandas 中的列(从头到尾)的最有效方法是什么?

python - openai使用python, "text-davinci-002"模型返回长度问题

python - 扭曲的 getPage() : process memory grow when requesting lot of pages

javascript - 从javascript中的点数组绘制SVG多边形

javascript - classList 在 MS-Edge 中不起作用

html - HTML 文件的输出不显示 C 生成代码中的图像