Python 基于文本删除SVG元素
我有一个SVG文件。我试图去除一些包含特定文本的元素:Python 基于文本删除SVG元素,python,svg,lxml,Python,Svg,Lxml,我有一个SVG文件。我试图去除一些包含特定文本的元素: <g style="font-family:'ARIAL'; stroke:none; fill:rgb(127,0,0);" > <g font-size="53.4132" > <text id="cv_126" x="168" y="474.78" transform="rotate(330 168 474.78) translate(168 -474.78) scale(1 1) translate(-
<g style="font-family:'ARIAL'; stroke:none; fill:rgb(127,0,0);" >
<g font-size="53.4132" >
<text id="cv_126" x="168" y="474.78" transform="rotate(330 168 474.78) translate(168 -474.78) scale(1 1) translate(-168 474.78) ">SomeSpecificText</text>
<text id="cv_127" x="336" y="474.78" transform="rotate(330 336 474.78) translate(336 -474.78) scale(1 1) translate(-336 474.78) ">SomeSpecificTextBis</text>
</g>
</g>
但我不知道该用哪种方法?我见过一些人谈论xpath,并尝试过,例如tree.xpath('.//g[contains(text(),“SomeSpecific”)])
,但它返回一个空列表
编辑
我尝试了以下方法,试图捕获包含“someSpecificText”(需要部分匹配)的结构,但它仍然为父对象返回空列表
tree = etree.parse(open("svg/myFile_ezdxf.svg"))
targets = tree.xpath('//g[./g[contains(text(),"SomeText")]]', namespaces = {"svg" : "http://www.w3.org/2000/svg"})
for target in targets:
target.getparent().remove(target)
这里还有我的svg文件的标题:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Generated by SomeCompanySoftware -->
<!-- www.somecompany.com -->
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg contentScriptType="text/ecmascript" xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify"
contentStyleType="text/css" preserveAspectRatio="xMidYMid meet"
width="840" height="593.48" viewBox="0 0 840 593.48"
version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:cvjs="http://www.somecompany.com/" stroke-linecap="round" stroke-linejoin="round" fill-rule="evenodd" >
您可以使用beutifulsoup4和python3来实现这一点。
在您的示例中,此代码将执行以下操作:
#!/usr/local/bin/python3
from bs4 import BeautifulSoup
tree = BeautifulSoup(open('svg.svg').read(),features="lxml")
for item in tree.find_all():
if item.getText().strip() == "SomeSpecificText" or item.getText().strip() == "SomeSpecificText" :
item.findParent().findParent().decompose()
print(tree)
虽然它有点脆弱,因为我不知道您的确切逻辑,但您可以改进它。您可以使用Beutiful Soup 4和Python 3来实现这一点。
在您的示例中,此代码将执行以下操作:
#!/usr/local/bin/python3
from bs4 import BeautifulSoup
tree = BeautifulSoup(open('svg.svg').read(),features="lxml")
for item in tree.find_all():
if item.getText().strip() == "SomeSpecificText" or item.getText().strip() == "SomeSpecificText" :
item.findParent().findParent().decompose()
print(tree)
虽然它有点脆弱,因为我不知道您的确切逻辑,但您可以改进它。使用lxml肯定可以做到:
targets = tree.xpath('//g[./g[text="SomeSpecificTextBis" or text="SomeSpecificText"]]')
for target in targets:
target.getparent().remove(target)
print(etree.tostring(tree, pretty_print=True).decode())
您完全可以使用lxml来实现:
targets = tree.xpath('//g[./g[text="SomeSpecificTextBis" or text="SomeSpecificText"]]')
for target in targets:
target.getparent().remove(target)
print(etree.tostring(tree, pretty_print=True).decode())
我找到了执行任务的方法:
tree = etree.parse(open("myFile.svg"))
root = tree.getroot()
targets = ["SomeText", "SomeText2"]
for element in root.iter("*"):
if (element.text is not None) and any([item in element.text for item in targets]):
element.getparent().remove(element)
with open('myModifiedFile.svg', 'wb') as f:
f.write(etree.tostring(tree))
我找到了执行任务的方法:
tree = etree.parse(open("myFile.svg"))
root = tree.getroot()
targets = ["SomeText", "SomeText2"]
for element in root.iter("*"):
if (element.text is not None) and any([item in element.text for item in targets]):
element.getparent().remove(element)
with open('myModifiedFile.svg', 'wb') as f:
f.write(etree.tostring(tree))
很可能您的xpath尝试不起作用,因为svg通常位于默认名称空间中。如果这不起作用,请尝试在您的问题中添加完整的svg开始标记(或者理想情况下,添加一个最小但完整的svg,以便我们可以复制)。不幸的是,我不能将整个svg放在一起,因为它包含许多敏感数据,而且我不能花时间使其匿名。我试过这个解决办法,但不起作用。我将更新我的帖子,说明我在您的编辑中尝试了什么,您正确绑定了“svg”前缀,但您没有在xpath中使用它。另外,text
是一个元素,因此在contains()中使用text()
将不起作用。我是这样做的:targets=tree.xpath('//svg:g[./svg:g[.//svg:text[contains(,“SomeSpecificText”)]][.//svg:text[contains(,“SomeSpecificTextBis”)]',命名空间={“svg”:http://www.w3.org/2000/svg“})
(通常不需要/
,但没有它们我什么都不会选)很可能您的xpath尝试不起作用,因为svg通常位于默认名称空间中。如果这不起作用,请尝试在您的问题中添加完整的svg开始标记(或者理想情况下,添加一个最小但完整的svg,以便我们可以复制)。不幸的是,我不能将整个svg放在一起,因为它包含许多敏感数据,而且我不能花时间使其匿名。我试过这个解决办法,但不起作用。我将更新我的帖子,说明我在您的编辑中尝试了什么,您正确绑定了“svg”前缀,但您没有在xpath中使用它。另外,text
是一个元素,因此在contains()中使用text()
将不起作用。我是这样做的:targets=tree.xpath('//svg:g[./svg:g[.//svg:text[contains(,“SomeSpecificText”)]][.//svg:text[contains(,“SomeSpecificTextBis”)]',命名空间={“svg”:http://www.w3.org/2000/svg“})
(通常不需要/
,但没有它们我什么都不会选)