在svg文件中使用python搜索和更改文本 [英] Search and alter text with python in a svg file

查看:230
本文介绍了在svg文件中使用python搜索和更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Inkscape创建的SVG文件,现在在svg文件中是我尝试更改的一些文本,但是如何? svg看起来像这样:

I've a SVG file created with Inkscape, now in the svg file is some text i try to alter but how? The svg looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <svg
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:cc="http://creativecommons.org/ns#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  width="1480"
  height="800"
  version="1.1"
  id="svg4184"
  sodipodi:docname="2017-05-03_heizung2.svg"
  inkscape:version="0.92.1 r15371">
  ...
  <g
   inkscape:groupmode="layer"
   inkscape:label="05_CURVES"
   id="g4182"
   transform="translate(0,-322.51962)"
   style="display:inline">
   ...
   <text
     xml:space="preserve"
     style="font-style:normal;font-weight:normal;font-size:37.33333206px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
     x="82.288353"
     y="920.41907"
     id="temp-bu"><tspan
       sodipodi:role="line"
       id="tspan5025-6-2-1-0"
       x="82.288353"
       y="920.41907"
       style="font-size:32px">TEXT TO CHANGE</tspan></text>
   ...
  </g>
 </svg>

我尝试使用xml.etree.ElementTree没有到达要更改的文本... 我也尝试过

I tried using xml.etree.ElementTree didn't get to the TEXT TO CHANGE... I also tried

from xml.dom as minidom
doc = minidom.parse('File.svg')
text = [text.getAttribute('id') for text in doc.getElementsByTagName('text')

但是我没办法更改文本...

but i didn't get to the TEXT TO CHANGE...

如何更改文本

推荐答案

要更改的文本在tspan标记中

TEXT TO CHANGE is in tspan tag

import xml.dom.minidom
doc = xml.dom.minidom.parse('File.svg')
name = doc.getElementsByTagName('tspan')
for t in name:
   if (t.attributes['id'].value=="tspan5025-6-2-1-0"):
    print [x.nodeValue for x in t.childNodes]

这篇关于在svg文件中使用python搜索和更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆