更新 SVG 文档中的特定属性

时间:2021-05-25 20:44:52

标签: python xml parsing svg elementtree

这是我的 XML/SVG 文档:

<ns0:svg xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns:ns0="http://www.w3.org/2000/svg"
         xmlns:ns1="http://www.inkscape.org/namespaces/inkscape"
         xmlns:ns2="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
         xmlns:ns4="http://creativecommons.org/ns#"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         id="svg2"
         style="clip-rule:evenodd;fill-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41420996"
         xml:space="preserve"
         version="1.1"
         viewBox="0 0 60 60"
         height="60"
         width="60"
         ns1:version="0.91 r13725"
         ns2:docname="atm.svg">
  <ns2:namedview pagecolor="#ffffff"
                 bordercolor="#666666"
                 borderopacity="1"
                 objecttolerance="10"
                 gridtolerance="10"
                 guidetolerance="10"
                 ns1:pageopacity="0"
                 ns1:pageshadow="2"
                 ns1:window-width="1649"
                 ns1:window-height="1069"
                 id="namedview19"
                 showgrid="false"
                 ns1:zoom="5.5625733"
                 ns1:cx="10.639262"
                 ns1:cy="37.744295"
                 ns1:window-x="1442"
                 ns1:window-y="405"
                 ns1:window-maximized="0"
                 ns1:current-layer="g2348" />
  <ns0:metadata id="metadata35">
    <rdf:RDF>
      <ns4:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title />
      </ns4:Work>
    </rdf:RDF>
  </ns0:metadata>
  <ns0:defs id="defs33" />
  <ns0:g transform="matrix(0.19997959,0,0,0.19995118,0.00612242,3.3238395e-4)"
         id="Background_circle">
    <ns0:g id="g5">
      <ns0:g id="g7">
        <ns0:g id="g9">
          <ns0:circle id="circle11"
                      style="fill:#ffffff"
                      r="150"
                      cy="150"
                      cx="150" />
          <ns0:path id="path13"
                    d="m 150.485,0.001 c 49.5244,-0.30805092 97.99005,26.13918 124.885,67.66 29.14949,43.41669 32.66869,102.35469 9.029,148.98 -22.3226,45.72399 -69.26524,78.27982 -120.017,82.677 C 116.79963,304.27157 67.626954,284.53159 36.597,248.141 4.2284143,211.43134 -7.8772602,158.34647 5.079,111.186 18.20087,60.591626 59.782145,18.758853 110.292,5.321 c 13.07378,-3.5887368 26.6371,-5.36778112 40.193,-5.32 z m -0.919,16 C 104.07954,15.682674 59.676029,40.73106 36.035,79.522 10.237529,120.258 9.3382219,175.08668 33.754,216.66 c 22.507056,39.83833 66.53845,66.428 112.351,67.284 44.86675,1.74378 89.5149,-21.18229 114.552,-58.394 27.38615,-39.18451 30.93913,-93.31402 9.052,-135.793 C 249.0838,48.105271 205.50868,18.954337 159.041,16.3 c -3.15359,-0.209033 -6.31449,-0.309606 -9.475,-0.299 z"
                    style="fill:#000000" />
        </ns0:g>
      </ns0:g>
    </ns0:g>
  </ns0:g>
  <ns0:g id="g2348"
         transform="matrix(20.299892,0,0,20.757616,28.738101,29.359515)">
    <ns0:path ns1:connector-curvature="0"
              id="path2341"
              d="m -0.57518956,-0.83795069 0,0.21003421 0.42006839,0 0.22976117,0.47460762 0.22860076,-0.47460762 0.41890798,0 0,-0.21003421 0.26805471,0.26689429 -0.26805471,0.26689428 0,-0.19030722 -0.3040274,0 -0.24832773,0.53262812 0.24832773,0.53262819 0.3040274,0 0,-0.19030726 0.26805471,0.2657339 -0.26805471,0.26689429 0,-0.19030723 -0.41890798,0 L 0.07464,0.2273056 l -0.22976117,0.49549507 -0.42006839,0 0,0.19030723 -0.26689423,-0.26689429 0.26689423,-0.2657339 0,0.19030726 0.28662125,0 0.26689429,-0.53262819 -0.26689429,-0.53262812 -0.28662125,0 0,0.19030722 -0.26689423,-0.26689428 0.26689423,-0.26689429"
              style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.001" />
  </ns0:g>
</ns0:svg>

我正在尝试专门将 style="fill:#000000; 更改为任何其他颜色,但问题是我的 XML 文档已经有很多 (style="fill:#123456;)。我如何才能将其缩小到我需要更改或指向它的确切位置?

这是我迄今为止尝试过的python代码:

import xml.etree.ElementTree as ET
with open('test.svg','r+') as f:
    tree = ET.parse(f)
    root = tree.getroot()
    for child in root:
        child.set('fill','#777777')

1 个答案:

答案 0 :(得分:0)

通过查看您的文件,我猜您的元素通常位于:root[4][0], 所以试试这个(或其中的一部分):

root[4][0].attrib['style'].split(';')[0].split('#')[1]

也可以通过id=g2348获取父元素

编辑:

root.find('./*[@id="g2348"]')[0].attrib['style']

或:

root.find('./*[@id="g2348"]')[0].attrib['style'] = 'fill:#123456;fill-opacity:1;stroke:none;stroke-width:0.001'
tree.write('test_2.svg')
相关问题