如何使用Python通过SVG文件中的id字段查找元素

2024-04-26 07:40:36 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是.svg文件(xml)的摘录:

   <text
       xml:space="preserve"
       style="font-size:14.19380379px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;font-family:DejaVu Sans Mono;-inkscape-font-specification:DejaVu Sans Mono"
       x="109.38555"
       y="407.02847"
       id="libcode-00"
       sodipodi:linespacing="125%"
       inkscape:label="#text4638"><tspan
         sodipodi:role="line"
         id="tspan4640"
         x="109.38555"
         y="407.02847">12345678</tspan></text>

我正在学习Python,不知道如何找到所有这样的text元素,它们的id字段等于libcode-XX,其中XX是一个数字。

我使用minidom的解析器加载了这个.svg文件,并尝试使用getElementById查找元素。但是我得到了None结果。

    svgTemplate = minidom.parse(svgFile)
    print svgTemplate
    print svgTemplate.getElementById('libcode-00')

接着问另一个问题,我试着在svgTemplate对象上使用setIdAttribute('id'),但没有成功。

一句话:请给出一个提示,说明一种聪明的方法来提取所有这些text元素,这些元素的形式是id。之后,获取tspan文本并用生成的内容替换它应该没有问题。


Tags: 文件textsvgid元素stylelinexml