lxml \u64cd\u4f5c svg \u56fe\u7247\u793a\u4f8b<\/strong><\/div>\n\u5728\u672c\u7bc7\u535a\u5ba2\u7684\u5165\u95e8\u7bc7\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u4e00\u4e0b lxml \u5e93\u89e3\u6790 svg \u6587\u4ef6\uff0c\u5e76\u4fee\u6539\u5b83\u7684\u989c\u8272\u3002<\/p>\n
SVG \u56fe\u7247\u751f\u6210\uff0c\u53ef\u4ee5\u67e5\u770b \u300aPython \u751f\u6210 svg \u56fe\u7247\uff0c\u4e00\u7bc7\u535a\u5ba2\u5e26\u4f60\u638c\u63e1 Python \u4e0e svg \u4e4b\u95f4\u7684\u64cd\u4f5c\u300b \u8fd9\u7bc7\u535a\u5ba2\u3002<\/p>\n
\u501f\u7528 svgwrite \u5e93\uff0c\u751f\u6210\u4e00\u4e2a\u7ea2\u8272\u80cc\u666f\u7684 svg \u56fe\u7247\u3002<\/p>\n
import svgwrite\r\n \r\ndwg = svgwrite.Drawing('demo.svg', size=(100, 20), profile='tiny')\r\ndwg.add(dwg.rect(insert=(0, 0), size=(100, 20), fill='red'))\r\nphone_number = '\u6a61\u76ae\u64e6'\r\ndwg.add(dwg.text(phone_number, insert=(0, 15), fill='white', font_size=12))\r\n \r\ndwg.save()<\/pre>\n
\n\u540c\u65f6\u67e5\u770b\u4e00\u4e0b svg \u5185\u5bb9\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n
< ?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n< svg baseProfile=\"tiny\" height=\"20\" version=\"1.2\" width=\"100\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"\r\n xmlns:ev=\"http:\/\/www.w3.org\/2001\/xml-events\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\">\r\n < defs\/>\r\n < rect fill=\"red\" height=\"20\" width=\"100\" x=\"0\" y=\"0\"\/>\r\n < text fill=\"white\" font-size=\"12\" x=\"0\" y=\"15\">\u6a61\u76ae\u64e6\r\n< \/svg><\/pre>\n\u6211\u4eec\u9700\u8981\u66ff\u6362\u7684\u5185\u5bb9\u662f fill=\"white\" \uff0c\u5c06\u5176\u4fee\u6539\u4e3a fill=\"#03a9f4\"\u3002\u4f7f\u7528 lxml \u8bfb\u53d6\u8be5\u6587\u4ef6\uff0c\u8fdb\u884c\u989c\u8272\u66ff\u6362\u3002<\/p>\n
from lxml import etree\r\n \r\n# \u89e3\u6790SVG\u6587\u4ef6\r\nsvg_file = 'demo.svg'\r\nwith open(svg_file, 'rb') as f:\r\n svg_data = f.read()\r\n parser = etree.XMLParser(remove_blank_text=True)\r\n svg_tree = etree.fromstring(svg_data, parser)\r\n \r\n# \u4fee\u6539\u989c\u8272\r\nfor element in svg_tree.iter():\r\n if 'fill' in element.attrib:\r\n # \u66ff\u6362\u989c\u8272\r\n element.attrib['fill'] = element.attrib['fill'].replace('white', '#03a9f4')\r\n \r\n# \u4fdd\u5b58\u4fee\u6539\u540e\u7684SVG\u6587\u4ef6\r\nwith open('modified_demo.svg', 'wb') as f:\r\n f.write(etree.tostring(svg_tree, pretty_print=True))<\/pre>\n\u6b64\u65f6\u66ff\u6362\u4e4b\u540e\uff0c\u518d\u6b21\u67e5\u770b svg \u56fe\u7247\u6e90\u7801\u5185\u5bb9\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n
< svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:ev=\"http:\/\/www.w3.org\/2001\/xml-events\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" baseProfile=\"tiny\" height=\"20\" version=\"1.2\" width=\"100\">\r\n < defs\/>\r\n < rect fill=\"red\" height=\"20\" width=\"100\" x=\"0\" y=\"0\"\/>\r\n < text fill=\"#03a9f4\" font-size=\"12\" x=\"0\" y=\"15\">橡皮擦\r\n< \/svg><\/pre>\n\u91cd\u70b9\u6ce8\u610f text \u5143\u7d20\u90e8\u5206\uff0c\u53d1\u73b0 fill \u5c5e\u6027\u7684\u503c\u5df2\u7ecf\u88ab\u4fee\u6539\u6210\u6700\u65b0\u7684\u989c\u8272\u4e86\u3002<\/p>\n
lxml \u7ed9 svg \u56fe\u7247\u6dfb\u52a0\u65b0\u5143\u7d20<\/strong><\/div>\n\u4f7f\u7528 append() \u51fd\u6570\u53ef\u4ee5\u7ed9 svg \u56fe\u7247\u6dfb\u52a0\u65b0\u5143\u7d20\uff0c\u4f8b\u5982\u4e0b\u8ff0\u4ee3\u7801\u5c06\u6dfb\u52a0\u4e00\u4e2a\u77e9\u5f62\u5230\u56fe\u7247\u4e2d\u3002<\/p>\n
from lxml import etree\r\n \r\n# \u89e3\u6790SVG\u6587\u4ef6\r\nsvg_file = 'demo.svg'\r\nwith open(svg_file, 'rb') as f:\r\n svg_data = f.read()\r\n parser = etree.XMLParser(remove_blank_text=True)\r\n svg_tree = etree.fromstring(svg_data, parser)\r\n \r\nnew_element = etree.Element('rect', x='5', y='5', width='50', height='20', style='fill:#03a9f4')\r\nsvg_tree.append(new_element)\r\n \r\n \r\n \r\n# \u4fdd\u5b58\u4fee\u6539\u540e\u7684SVG\u6587\u4ef6\r\nwith open('modified_demo.svg', 'wb') as f:\r\n f.write(etree.tostring(svg_tree, pretty_print=True))<\/pre>\n\u8fd9\u91cc\u77e9\u5f62\u7684\u5b9a\u4f4d\u5e76\u6ca1\u6709\u7cbe\u786e\u8ba1\u7b97\uff0c\u5b9e\u73b0\u7684\u6548\u679c\u56fe\u5982\u4e0b\u6240\u793a\u3002<\/p>\n
<\/p>\n
lxml \u5220\u9664 svg \u56fe\u7247\u4e2d\u7684\u5143\u7d20<\/strong><\/div>\n\u9664\u65b0\u589e\u5916\uff0c\u8fd8\u53ef\u4ee5\u5bf9 svg \u4e2d\u5143\u7d20\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n
from lxml import etree\r\n \r\n# \u89e3\u6790SVG\u6587\u4ef6\r\nsvg_file = 'modified_demo.svg'\r\nwith open(svg_file, 'r') as f:\r\n svg_data = f.read()\r\n parser = etree.XMLParser(remove_blank_text=True)\r\n svg_tree = etree.fromstring(svg_data, parser)\r\n \r\n# \u901a\u8fc7 xpath \u67e5\u627e\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\r\nelements_to_remove = svg_tree.xpath(\"\/\/text\")\r\nprint(elements_to_remove)\r\nfor element in elements_to_remove:\r\n element.getparent().remove(element)<\/pre>\n\u8fd0\u884c\u4ee3\u7801\u4f1a\u53d1\u73b0\u901a\u8fc7 xpath \u65e0\u6cd5\u67e5\u627e\u5230\u76ee\u6807\u5143\u7d20 text\uff0c\u8fd9\u65f6\u56e0\u4e3a SVG \u6587\u4ef6\u4e2d\u542b\u6709\u547d\u540d\u7a7a\u95f4\uff0c\u5bfc\u81f4 xpath \u8bed\u6cd5\u67e5\u8be2\u4e0d\u5230\u76f8\u5e94\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e3a xpath \u8bed\u6cd5\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n
\u6dfb\u52a0\u547d\u540d\u7a7a\u95f4\u4e4b\u540e\u7684\u63d0\u53d6\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\u3002<\/p>\n
from lxml import etree\r\n \r\n# \u89e3\u6790SVG\u6587\u4ef6\r\nsvg_file = 'modified_demo.svg'\r\nsvg_tree = etree.parse(svg_file)\r\nroot = svg_tree.getroot()\r\n \r\n \r\n# \u83b7\u53d6\u547d\u540d\u7a7a\u95f4\r\nns = {'svg': root.nsmap[None]}\r\n# \u901a\u8fc7 xpath \u67e5\u627e\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\r\nelements_to_remove = svg_tree.xpath(\"\/\/svg:text\", namespaces=ns)\r\n \r\nprint(elements_to_remove)<\/pre>\n\u5c24\u5176\u6ce8\u610f xpath \u90e8\u5206\u9700\u8981\u4f7f\u7528 \/\/svg:text \u8fdb\u884c\u63d0\u53d6\u3002<\/p>\n
\u5982\u679c\u4e0d\u63d0\u524d\u751f\u547d ns \u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u8ff0\u4ee3\u7801\u8fdb\u884c\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n
elements_to_remove = svg_tree.xpath(\"\/\/svg:circle\", namespaces={'svg': 'http:\/\/www.w3.org\/2000\/svg'})<\/pre>\n\u6709\u4e86\u4e0a\u8ff0\u57fa\u7840\u4e4b\u540e\uff0c\u5b8c\u6574\u7684 lxml \u5220\u9664\u5143\u7d20\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n
from lxml import etree\r\n \r\n# \u89e3\u6790SVG\u6587\u4ef6\r\nsvg_file = 'modified_demo.svg'\r\nsvg_tree = etree.parse(svg_file)\r\nroot = svg_tree.getroot()\r\n \r\n \r\n# \u83b7\u53d6\u547d\u540d\u7a7a\u95f4\r\nns = {'svg': root.nsmap[None]}\r\n# \u901a\u8fc7 xpath \u67e5\u627e\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\r\nelements_to_remove = svg_tree.xpath(\"\/\/svg:text\", namespaces=ns)\r\n \r\nprint(elements_to_remove)\r\nfor element in elements_to_remove:\r\n element.getparent().remove(element)\r\n \r\n# \u4fdd\u5b58\u4fee\u6539\u540e\u7684 SVG \u6587\u4ef6\r\nwith open('modified_demo1.svg', 'wb') as f:\r\n f.write(etree.tostring(svg_tree, pretty_print=True))<\/pre>\n\u4ee5\u4e0a\u5c31\u662fpython\u5229\u7528lxml\u5e93\u5269\u4e0b\u64cd\u4f5csvg\u56fe\u7247\u7684\u8be6\u7ec6\u5185\u5bb9<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u5927\u591a\u6570\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u90fd\u7528 lxml \u5e93\u89e3\u6790\u7f51\u9875\u6e90\u7801\uff0c\u4f46\u4f60\u662f\u5426\u77e5\u9053\uff0clxml \u5e93\u4e5f\u662f\u53ef\u4ee5\u64cd\u4f5c svg \u56fe\u7247\u7684\u3002\u6211 […]<\/p>\n","protected":false},"author":362,"featured_media":176469,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-262855","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=262855"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262855\/revisions"}],"predecessor-version":[{"id":262878,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262855\/revisions\/262878"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/176469"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=262855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=262855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=262855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}