{"id":949057,"date":"2024-12-27T00:09:21","date_gmt":"2024-12-26T16:09:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949057.html"},"modified":"2024-12-27T00:09:23","modified_gmt":"2024-12-26T16:09:23","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5svg","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/949057.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165svg"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084158\/b9bad984-aece-4b17-8e84-397b4723177e.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165svg\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5bfc\u5165SVG\u6587\u4ef6\u5e76\u8fdb\u884c\u5904\u7406\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\u548c\u9879\u76ee\u7684\u590d\u6742\u6027\u3002<strong>\u5e38\u7528\u7684\u5e93\u5305\u62ecsvgwrite\u3001c<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rosvg\u3001svgpathtools\u548clxml<\/strong>\uff0c\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u7528\u9014\u3002<strong>svgwrite\u7528\u4e8e\u521b\u5efaSVG\u6587\u4ef6\u3001cairosvg\u7528\u4e8e\u5c06SVG\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u3001svgpathtools\u4e13\u6ce8\u4e8e\u89e3\u6790\u548c\u5904\u7406SVG\u8def\u5f84\u6570\u636e\u3001lxml\u5219\u7528\u4e8e\u89e3\u6790\u548c\u4fee\u6539XML\u683c\u5f0f\u7684SVG\u6587\u4ef6<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SVGWRITE\u521b\u5efa\u548c\u7f16\u8f91SVG\u6587\u4ef6<\/p>\n<\/p>\n<p><p>svgwrite\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efaSVG\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u5141\u8bb8\u60a8\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u751f\u6210SVG\u56fe\u50cf\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u52a8\u6001\u751f\u6210\u77e2\u91cf\u56fe\u5f62\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5svgwrite\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install svgwrite<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528svgwrite\u521b\u5efaSVG\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u77e9\u5f62\u548c\u5706\u5f62\u7684SVG\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import svgwrite<\/p>\n<p>def create_svg():<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2aSVG\u5bf9\u8c61<\/p>\n<p>    dwg = svgwrite.Drawing(&#39;example.svg&#39;, profile=&#39;tiny&#39;)<\/p>\n<p>    # \u6dfb\u52a0\u77e9\u5f62<\/p>\n<p>    dwg.add(dwg.rect(insert=(10, 10), size=(100, 50), fill=&#39;blue&#39;))<\/p>\n<p>    # \u6dfb\u52a0\u5706\u5f62<\/p>\n<p>    dwg.add(dwg.circle(center=(150, 30), r=20, fill=&#39;red&#39;))<\/p>\n<p>    # \u4fdd\u5b58SVG\u6587\u4ef6<\/p>\n<p>    dwg.save()<\/p>\n<p>create_svg()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.svg<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7ed8\u5236\u4e86\u4e00\u4e2a\u84dd\u8272\u77e9\u5f62\u548c\u4e00\u4e2a\u7ea2\u8272\u5706\u5f62\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9ad8\u7ea7\u529f\u80fd<\/li>\n<\/ol>\n<p><p>svgwrite\u8fd8\u652f\u6301\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u6e10\u53d8\u3001\u6837\u5f0f\u548c\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5305\u542b\u7ebf\u6027\u6e10\u53d8\u548c\u6587\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_advanced_svg():<\/p>\n<p>    dwg = svgwrite.Drawing(&#39;advanced_example.svg&#39;, profile=&#39;tiny&#39;)<\/p>\n<p>    # \u5b9a\u4e49\u7ebf\u6027\u6e10\u53d8<\/p>\n<p>    linear_gradient = dwg.defs.add(dwg.linearGradient(id=&#39;grad1&#39;, x1=&#39;0%&#39;, y1=&#39;0%&#39;, x2=&#39;100%&#39;, y2=&#39;0%&#39;))<\/p>\n<p>    linear_gradient.add_stop_color(offset=&#39;0%&#39;, color=&#39;blue&#39;, opacity=1)<\/p>\n<p>    linear_gradient.add_stop_color(offset=&#39;100%&#39;, color=&#39;green&#39;, opacity=1)<\/p>\n<p>    # \u4f7f\u7528\u6e10\u53d8\u586b\u5145\u77e9\u5f62<\/p>\n<p>    dwg.add(dwg.rect(insert=(10, 10), size=(100, 50), fill=&#39;url(#grad1)&#39;))<\/p>\n<p>    # \u6dfb\u52a0\u6587\u672c<\/p>\n<p>    dwg.add(dwg.text(&#39;Hello SVG&#39;, insert=(10, 100), fill=&#39;black&#39;, font_size=&#39;20px&#39;))<\/p>\n<p>    dwg.save()<\/p>\n<p>create_advanced_svg()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7ebf\u6027\u6e10\u53d8\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u4e00\u4e2a\u77e9\u5f62\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u5728SVG\u4e2d\u6dfb\u52a0\u4e86\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528CAIROSVG\u8f6c\u6362SVG\u6587\u4ef6<\/p>\n<\/p>\n<p><p>CairoSVG\u662f\u4e00\u4e2aSVG\u5230PNG\u3001PDF\u3001PS\u7b49\u683c\u5f0f\u7684\u8f6c\u6362\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u5c06SVG\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5cairosvg\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cairosvg<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528cairosvg\u5c06SVG\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06SVG\u8f6c\u6362\u4e3aPNG\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cairosvg<\/p>\n<p>def convert_svg_to_png(svg_input, png_output):<\/p>\n<p>    cairosvg.svg2png(url=svg_input, write_to=png_output)<\/p>\n<p>convert_svg_to_png(&#39;example.svg&#39;, &#39;output.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06<code>example.svg<\/code>\u6587\u4ef6\u8f6c\u6362\u4e3a<code>output.png<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8f6c\u6362\u4e3aPDF\u548c\u5176\u4ed6\u683c\u5f0f<\/li>\n<\/ol>\n<p><p>CairoSVG\u4e0d\u4ec5\u53ef\u4ee5\u5c06SVG\u8f6c\u6362\u4e3aPNG\uff0c\u8fd8\u652f\u6301\u8f6c\u6362\u4e3aPDF\u548cPS\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u8f6c\u6362\u4e3aPDF\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_svg_to_pdf(svg_input, pdf_output):<\/p>\n<p>    cairosvg.svg2pdf(url=svg_input, write_to=pdf_output)<\/p>\n<p>convert_svg_to_pdf(&#39;example.svg&#39;, &#39;output.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>svg2ps<\/code>\u51fd\u6570\u5c06SVG\u8f6c\u6362\u4e3aPS\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528SVGPATHTOOLS\u89e3\u6790\u548c\u5904\u7406SVG\u8def\u5f84<\/p>\n<\/p>\n<p><p>svgpathtools\u662f\u4e00\u4e2a\u4e13\u6ce8\u4e8e\u89e3\u6790\u548c\u5904\u7406SVG\u8def\u5f84\u6570\u636e\u7684Python\u5e93\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u8def\u5f84\u64cd\u4f5c\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5svgpathtools\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install svgpathtools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528svgpathtools\u89e3\u6790SVG\u6587\u4ef6\u4e2d\u7684\u8def\u5f84\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u89e3\u6790\u8def\u5f84\u5e76\u8ba1\u7b97\u8def\u5f84\u957f\u5ea6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from svgpathtools import svg2paths<\/p>\n<p>def parse_svg_paths(svg_input):<\/p>\n<p>    # \u89e3\u6790SVG\u6587\u4ef6\u4e2d\u7684\u8def\u5f84<\/p>\n<p>    paths, attributes = svg2paths(svg_input)<\/p>\n<p>    # \u8f93\u51fa\u6bcf\u4e2a\u8def\u5f84\u7684\u957f\u5ea6<\/p>\n<p>    for i, path in enumerate(paths):<\/p>\n<p>        print(f&#39;Path {i} length: {path.length()}&#39;)<\/p>\n<p>parse_svg_paths(&#39;example.svg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u89e3\u6790\u4e86<code>example.svg<\/code>\u6587\u4ef6\u4e2d\u7684\u8def\u5f84\uff0c\u5e76\u8f93\u51fa\u4e86\u6bcf\u4e2a\u8def\u5f84\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u64cd\u4f5c\u8def\u5f84<\/li>\n<\/ol>\n<p><p>svgpathtools\u8fd8\u652f\u6301\u5bf9\u8def\u5f84\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u5e73\u79fb\u3001\u7f29\u653e\u548c\u65cb\u8f6c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6240\u6709\u8def\u5f84\u5e73\u79fb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from svgpathtools import Path, Line<\/p>\n<p>def translate_paths(svg_input, svg_output, dx, dy):<\/p>\n<p>    paths, attributes = svg2paths(svg_input)<\/p>\n<p>    # \u5e73\u79fb\u8def\u5f84<\/p>\n<p>    translated_paths = [path.translated(complex(dx, dy)) for path in paths]<\/p>\n<p>    # \u4fdd\u5b58\u65b0\u7684SVG\u6587\u4ef6<\/p>\n<p>    with open(svg_output, &#39;w&#39;) as f:<\/p>\n<p>        f.write(f&#39;&lt;svg xmlns=&quot;http:\/\/www.w3.org\/2000\/svg&quot;&gt;\\n&#39;)<\/p>\n<p>        for path in translated_paths:<\/p>\n<p>            f.write(f&#39;  &lt;path d=&quot;{path.d()}&quot; \/&gt;\\n&#39;)<\/p>\n<p>        f.write(&#39;&lt;\/svg&gt;&#39;)<\/p>\n<p>translate_paths(&#39;example.svg&#39;, &#39;translated_example.svg&#39;, 10, 20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8def\u5f84\u5e73\u79fb\u4e86(10, 20)\u4e2a\u5355\u4f4d\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684SVG\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528LXML\u89e3\u6790\u548c\u4fee\u6539SVG\u6587\u4ef6<\/p>\n<\/p>\n<p><p>lxml\u662f\u4e00\u4e2a\u5f3a\u5927\u7684XML\u5904\u7406\u5e93\uff0c\u9002\u5408\u9700\u8981\u89e3\u6790\u548c\u4fee\u6539SVG\u6587\u4ef6\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5lxml\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528lxml\u89e3\u6790SVG\u6587\u4ef6\u5e76\u8fdb\u884c\u4fee\u6539\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4fee\u6539SVG\u6587\u4ef6\u4e2d\u6240\u6709\u77e9\u5f62\u989c\u8272\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<p>def modify_svg_rectangles(svg_input, svg_output, new_color):<\/p>\n<p>    # \u89e3\u6790SVG\u6587\u4ef6<\/p>\n<p>    tree = etree.parse(svg_input)<\/p>\n<p>    root = tree.getroot()<\/p>\n<p>    # \u4fee\u6539\u6240\u6709\u77e9\u5f62\u7684\u989c\u8272<\/p>\n<p>    for rect in root.findall(&#39;.\/\/{http:\/\/www.w3.org\/2000\/svg}rect&#39;):<\/p>\n<p>        rect.set(&#39;fill&#39;, new_color)<\/p>\n<p>    # \u4fdd\u5b58\u4fee\u6539\u540e\u7684SVG\u6587\u4ef6<\/p>\n<p>    tree.write(svg_output)<\/p>\n<p>modify_svg_rectangles(&#39;example.svg&#39;, &#39;modified_example.svg&#39;, &#39;purple&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06<code>example.svg<\/code>\u6587\u4ef6\u4e2d\u6240\u6709\u77e9\u5f62\u7684\u586b\u5145\u989c\u8272\u4fee\u6539\u4e3a\u7d2b\u8272\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684SVG\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9ad8\u7ea7\u89e3\u6790\u548c\u4fee\u6539<\/li>\n<\/ol>\n<p><p>lxml\u8fd8\u652f\u6301\u66f4\u9ad8\u7ea7\u7684\u89e3\u6790\u548c\u4fee\u6539\u64cd\u4f5c\uff0c\u4f8b\u5982\u67e5\u627e\u7279\u5b9a\u5c5e\u6027\u6216\u5143\u7d20\u3001\u6dfb\u52a0\u65b0\u5143\u7d20\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5411SVG\u6587\u4ef6\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_element_to_svg(svg_input, svg_output):<\/p>\n<p>    tree = etree.parse(svg_input)<\/p>\n<p>    root = tree.getroot()<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5706\u5f62\u5143\u7d20<\/p>\n<p>    new_circle = etree.Element(&#39;circle&#39;, cx=&#39;50&#39;, cy=&#39;50&#39;, r=&#39;40&#39;, fill=&#39;yellow&#39;)<\/p>\n<p>    # \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230SVG\u4e2d<\/p>\n<p>    root.append(new_circle)<\/p>\n<p>    # \u4fdd\u5b58\u4fee\u6539\u540e\u7684SVG\u6587\u4ef6<\/p>\n<p>    tree.write(svg_output)<\/p>\n<p>add_element_to_svg(&#39;example.svg&#39;, &#39;new_element_example.svg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5411<code>example.svg<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u9ec4\u8272\u5706\u5f62\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684SVG\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u5165\u548c\u5904\u7406SVG\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\u3002<strong>svgwrite\u9002\u5408\u521b\u5efaSVG\u6587\u4ef6\uff0ccairosvg\u7528\u4e8e\u683c\u5f0f\u8f6c\u6362\uff0csvgpathtools\u4e13\u6ce8\u4e8e\u8def\u5f84\u89e3\u6790\u548c\u64cd\u4f5c\uff0clxml\u5219\u63d0\u4f9b\u5f3a\u5927\u7684XML\u89e3\u6790\u548c\u4fee\u6539\u529f\u80fd<\/strong>\u3002\u6839\u636e\u9879\u76ee\u7684\u4e0d\u540c\u9700\u6c42\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u8fdb\u884c\u5b9e\u73b0\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u8fd8\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u5de5\u5177\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684SVG\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165SVG\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5bfc\u5165SVG\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\uff0c\u5982<code>svgpathtools<\/code>\u548c<code>cairosvg<\/code>\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6SVG\u6587\u4ef6\u7684\u5185\u5bb9\u3001\u8fdb\u884c\u4fee\u6539\u6216\u5c06\u5176\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u76f8\u5e94\u5e93\u3001\u52a0\u8f7dSVG\u6587\u4ef6\u5e76\u4f7f\u7528\u76f8\u5173\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5bfc\u5165SVG\u6587\u4ef6\u6709\u54ea\u4e9b\u5e38\u89c1\u5e93\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u5e93\u5305\u62ec<code>svgwrite<\/code>\u3001<code>svgpathtools<\/code>\u548c<code>cairosvg<\/code>\u3002<code>svgwrite<\/code>\u53ef\u7528\u4e8e\u521b\u5efa\u548c\u7f16\u8f91SVG\u6587\u4ef6\uff0c\u800c<code>svgpathtools<\/code>\u5219\u9002\u5408\u5904\u7406\u8def\u5f84\u6570\u636e\u3002<code>cairosvg<\/code>\u53ef\u4ee5\u5c06SVG\u8f6c\u6362\u4e3aPNG\u6216PDF\u683c\u5f0f\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u5173\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u663e\u793aSVG\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>IPython.display<\/code>\u6a21\u5757\u4e2d\u7684<code>SVG<\/code>\u548c<code>Image<\/code>\u51fd\u6570\u6765\u663e\u793aSVG\u56fe\u50cf\u3002\u5982\u679c\u60a8\u5728Jupyter Notebook\u4e2d\u5de5\u4f5c\uff0c\u76f4\u63a5\u4f7f\u7528<code>display(SVG(&#39;yourfile.svg&#39;))<\/code>\u5373\u53ef\u8f7b\u677e\u663e\u793aSVG\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u5c06SVG\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u56fe\u50cf\u683c\u5f0f\u540e\u518d\u8fdb\u884c\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165SVG\u6587\u4ef6\u5e76\u8fdb\u884c\u5904\u7406\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\u548c\u9879\u76ee\u7684\u590d\u6742\u6027\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecsvg [&hellip;]","protected":false},"author":3,"featured_media":949066,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949057"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=949057"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949057\/revisions"}],"predecessor-version":[{"id":949069,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949057\/revisions\/949069"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/949066"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=949057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=949057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=949057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}