![](/img/trans.png)
[英]How to find elements by 'id' field in SVG file using Python
[英]How to remove invisible elements in SVG files using Python?
如何通過使用Python腳本刪除不可見的對象或圖層來清理SVG文件。 此類元素至少具有以下樣式之一:
display: none
opacity: 0
visibility: hidden
我正在考慮使用正則表達式模式,但考慮到XML的復雜性,通常不鼓勵使用正則表達式。
是否可以選擇使用lxml? 我可以做一些HTML修改,但我不確定SVG以及如何將修改后的數據正確保存到文件中。
完成了:
from lxml import etree
doc = etree.parse('/test.svg')
for action, el in etree.iterwalk(doc):
if el.attrib.get('display', None) == 'none' or el.attrib.get('visibility', None) == 'hidden' or el.attrib.get('opacity', None) == '0':
el.clear()
with open('/test_out.svg', 'w') as f:
f.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'+etree.tostring(doc, pretty_print=True))
我希望能抓住所有案件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.