【问题标题】:SVG file. remove elementSVG 文件。移除元素
【发布时间】:2018-10-02 04:51:14
【问题描述】:

我正在尝试删除 ID 为“area_3”的元素。我用过类似的东西:

for node in tree.xpath('//ellipse'):
    node.getparent().remove(node)

SVG 示例:

<svg width="600" height="600" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
     <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
     <g>
      <title>Layer 1</title>
      <image id="svg_1" y="0" x="0"/>
      <image stroke="null" xlink:href="tehplan.jpg" id="svg_5" height="587.777769" width="585.333339" y="0.578137" x="20.083334"/>
      <ellipse ry="19" rx="18" id="area_2" cy="172" cx="189" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke="#000000" fill="#ffffff"/>
      <ellipse id="area_3" ry="19" rx="18" cy="161" cx="275" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke="#000000" fill="#ffffff"/>
     </g>
    </svg>

【问题讨论】:

    标签: python svg lxml


    【解决方案1】:

    试试这个:

    from lxml import etree
    
    tree = etree.parse(open("so.svg"))
    to_remove = tree.xpath("/svg:svg/svg:g/svg:ellipse[@id=\"area_3\"]",
      namespaces={"svg": "http://www.w3.org/2000/svg"})[0]
    g = to_remove.getparent()
    g.remove(to_remove)
    with open("so.out.svg", "wb") as o:
        o.write(etree.tostring(tree, pretty_print=True))
    

    输出:

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="600">
         <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
         <g>
          <title>Layer 1</title>
          <image id="svg_1" y="0" x="0"/>
          <image stroke="null" xlink:href="tehplan.jpg" id="svg_5" height="587.777769" width="585.333339" y="0.578137" x="20.083334"/>
          <ellipse ry="19" rx="18" id="area_2" cy="172" cx="189" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke="#000000" fill="#ffffff"/>
          </g>
        </svg>
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 2020-05-19
      • 2013-06-19
      • 1970-01-01
      • 2012-10-31
      • 2015-10-03
      • 1970-01-01
      相关资源
      最近更新 更多