Select 使用 lxml 按组 ID 分组的 SVG 路径
Select SVG paths of group by group id using lxml
我在使用 lxml select设置一组特定路径时遇到问题。 SVG 结构如下所示
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (http://matplotlib.org/) -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="288pt" version="1.1" viewBox="0 0 432 288" width="432pt">
<defs>
<style type="text/css">
*{stroke-linecap:butt;stroke-linejoin:round;}
</style>
</defs>
<g id="figure_1">
<g id="patch_1">
<path d=" M0 288 L432 288 L432 0 L0 0 z " style="fill:#ffffff;"/>
</g>
<g id="patch_2">
<path d=" M0 288 L432 288 L432 0 L0 0 z " style="fill:#ffffff;"/>
</g>
<g id="axes_1">
<g id="Poly3DCollection_1">
<path clip-path="url(#pe61355d493)" d=" M195.211 34.2225 L194.801 34.0894 L196.527 212.986 L196.909 212.999 z " style="fill:#0000ff;"/>
<path clip-path="url(#pe61355d493)" d=" M195.504 34.3231 L195.211 34.2225 L196.909 212.999 L197.184 213.022 z " style="fill:#0000ff;"/>
...
它是底部列出的路径,我想 select 并更改它们的样式,但我似乎无法获得正确的语法并且我未能 select 路径
ifilename = "myfig.svg"
with open( ifilename, 'r') as infile:
tree = etree.parse( infile )
elements = tree.findall(".//g[@id='Poly3DCollection_1'")
new_style = 'stroke-width:4px; stroke: linear-gradient(orange, darkblue)'
for child in elements:
child.attrib['style'] = new_style
mod_svg = 'myfigmod.svg'
tree.write(mod_svg)
编辑
所以这让我在这个例子中得到了我想要的元素,但我仍然想要一种获取这个元素的特定方法
root = tree.getroot()
for child in root[1][2][0]:
child.attrib['style'] = new_style
etree中没有get_element_by_id,所以你得用xpath,就像你抓取元素一样。我创建了您的文件和 运行 下面的代码,并且能够更改组的样式。
element = tree.findall(".//{%s}g[@id='Poly3DCollection_1']" % SVG_NS)[0]
element.attrib["style"] = new_style
我在使用 lxml select设置一组特定路径时遇到问题。 SVG 结构如下所示
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (http://matplotlib.org/) -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="288pt" version="1.1" viewBox="0 0 432 288" width="432pt">
<defs>
<style type="text/css">
*{stroke-linecap:butt;stroke-linejoin:round;}
</style>
</defs>
<g id="figure_1">
<g id="patch_1">
<path d=" M0 288 L432 288 L432 0 L0 0 z " style="fill:#ffffff;"/>
</g>
<g id="patch_2">
<path d=" M0 288 L432 288 L432 0 L0 0 z " style="fill:#ffffff;"/>
</g>
<g id="axes_1">
<g id="Poly3DCollection_1">
<path clip-path="url(#pe61355d493)" d=" M195.211 34.2225 L194.801 34.0894 L196.527 212.986 L196.909 212.999 z " style="fill:#0000ff;"/>
<path clip-path="url(#pe61355d493)" d=" M195.504 34.3231 L195.211 34.2225 L196.909 212.999 L197.184 213.022 z " style="fill:#0000ff;"/>
...
它是底部列出的路径,我想 select 并更改它们的样式,但我似乎无法获得正确的语法并且我未能 select 路径
ifilename = "myfig.svg"
with open( ifilename, 'r') as infile:
tree = etree.parse( infile )
elements = tree.findall(".//g[@id='Poly3DCollection_1'")
new_style = 'stroke-width:4px; stroke: linear-gradient(orange, darkblue)'
for child in elements:
child.attrib['style'] = new_style
mod_svg = 'myfigmod.svg'
tree.write(mod_svg)
编辑
所以这让我在这个例子中得到了我想要的元素,但我仍然想要一种获取这个元素的特定方法
root = tree.getroot()
for child in root[1][2][0]:
child.attrib['style'] = new_style
etree中没有get_element_by_id,所以你得用xpath,就像你抓取元素一样。我创建了您的文件和 运行 下面的代码,并且能够更改组的样式。
element = tree.findall(".//{%s}g[@id='Poly3DCollection_1']" % SVG_NS)[0]
element.attrib["style"] = new_style