在SVG Python中更改路径类名称 [英] Changing a path class name in SVG Python

查看:76
本文介绍了在SVG Python中更改路径类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是此处问题的直接跟进: 在给定整数参数Python时进行字符串操作

This question is a direct follow up to the question right here: String manipulation when given an integer parameter Python

我会解释.我有这个SVG图片(只是一个例子):

I'll explan. I have this SVG image (just an example):

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 4000">
<defs>
    <style>.cls-1{opacity:0.05;}.cls-2{opacity:0.1;}.cls-3{opacity:0.2;}.cls-4{opacity:0.08;}</style>
</defs>
<title>shader</title>
<g id="shader">
    <path class="cls-1" d="M1850,1434.82a211.94,211.94,0,0,0-49.86-12.11c-172.54-20-337.7,181.45-333.37,371.56,2.51,110,62.25,236.26,159.68,295.51-121-45.56-197-190.72-199.85-315.16C1422.3,1584.51,1587.45,1383,1760,1403.06,1792.12,1406.79,1822.38,1418,1850,1434.82Z" />
    <path class="cls-1" d="M2575.47,1764.59c-.67,124.47-74.15,271-194.33,318.62,96.39-61,153.92-188.21,154.5-298.27,1-190.16-167.63-388.72-339.79-365.68a212.08,212.08,0,0,0-49.68,13c27.35-17.35,57.43-29,89.5-33.34C2407.84,1375.87,2576.48,1574.44,2575.47,1764.59Z" />
    <path class="cls-2" d="M1869.31,1609a158.22,158.22,0,0,0-18.56-4.78c-98.1-19-195.56,57.9-217.67,171.85-17.27,89,16.5,174.22,78.84,216.37-87.16-28.22-139.71-129.44-119-236,22.12-114,119.58-190.89,217.68-171.85A158.2,158.2,0,0,1,1869.31,1609Z" />
    <path class="cls-2" d="M2294.05,1987.37c61.58-43.24,93.85-129,75-217.7-24.1-113.55-122.89-188.78-220.64-168a156.86,156.86,0,0,0-18.48,5.11,158.48,158.48,0,0,1,58.3-25.46c97.76-20.75,196.54,54.48,220.65,168C2431.45,1855.52,2380.68,1957.62,2294.05,1987.37Z" />
    <path class="cls-3" d="M2214.61,2081.59c0,74.18-105.25,164.48-213.18,165.42-117.12,1-215.68-90.36-215.68-165.63a55.33,55.33,0,0,1,1.46-12.65c39.94,53.41,121.8,99,214,98.28,85.64-.67,169.74-46.83,210.9-100.17A45.83,45.83,0,0,1,2214.61,2081.59Z" />
    <path class="cls-4" d="M1097,2439.73c-83.85-36.5-164-99.38-215-176-77.46-116.54-125.85-323-16.55-426.93,45.17-43,107.55-59.14,173.62-56q-.82,10.15-1.4,20.33c-50.28,4.26-96.49,21.49-132,55.29-109.31,103.91-60.91,310.39,16.54,426.93C964.94,2347.64,1028.26,2402.24,1097,2439.73Z" />
    <path class="cls-4" d="M3125.8,2259.28c-49.63,77.57-128.8,141.88-212.06,179.83,68.19-38.68,130.6-94.42,172.23-159.49,75.41-117.87,120.2-325.16,9.09-427.16-35.33-32.43-80.57-48.85-129.62-52.73q-.36-10.47-.91-20.79c64.45-3.41,125.43,11.93,170.36,53.18C3246,1934.11,3201.21,2141.4,3125.8,2259.28Z" />
    <path class="cls-2" d="M2247.57,3056.69C2229,3156,2139.35,3296.86,2001.43,3298c-135,1.16-227.85-132.31-245.4-235.86a178.64,178.64,0,0,1-1.42-47.38c.3,2.46.65,4.92,1.07,7.38,17.55,103.55,110.38,237,245.4,235.86,137.92-1.18,227.6-142.05,246.14-241.34,1.07-5.76,1.87-11.52,2.38-17.35C2251.54,3019.91,2251,3038.33,2247.57,3056.69Z" />
    <g class="cls-3">
        <path d="M2267.51,3173.33c-11.06,5.09-30.83,7.25-66-2.54q3.45-5.48,6.65-11c24.86,7.24,42.33,8.69,54.58,7.3A52.66,52.66,0,0,0,2267.51,3173.33Z" />
        <path d="M2566.83,3042.84a38.52,38.52,0,0,0,9.66-5.13,41.64,41.64,0,0,1-19.57,15.22c-29.25,10.51-62.64-17.51-63-17.8a18.94,18.94,0,0,0-19.87-2.93,19.13,19.13,0,0,1,3.08-5,18.94,18.94,0,0,1,26.7-2.14C2504.19,3025.33,2537.58,3053.35,2566.83,3042.84Z" />
        <path d="M2632.39,3018.8c-6,19.09-16,34.68-28.85,45.72a105.14,105.14,0,0,0,18.94-35.63c7.32-23.08,8.67-50.8,3.61-74.15-6.17-28.49-22.66-54.2-47.68-74.34-21.8-17.55-48.41-29.47-74.94-33.58-41.18-6.37-84.4,5.86-125,35.36a247,247,0,0,0-26.12,22,250.49,250.49,0,0,1,36-32.06c40.59-29.5,83.81-41.72,125-35.36,26.53,4.11,53.14,16,74.94,33.58,25,20.14,41.51,45.85,47.69,74.34C2641.06,2968,2639.71,2995.72,2632.39,3018.8Z" />
    </g>
    <path class="cls-1" d="M1360.7,2126.07c11.9-10.67-113.73-80.72-109.94-279,.11-5.45.07-10.91.22-16.37,8.5,180.23,141.67,258.15,141.84,277.92C1392.85,2112.33,1373.28,2120.71,1360.7,2126.07Z" />
    <path class="cls-1" d="M2610.35,2101.65c-.2-23.12,135.56-86.47,140.69-279.55.35,8.32.14,16.65.54,25,9.56,197.82-123.27,264.93-110.31,278.16C2627.73,2121,2610.39,2105.61,2610.35,2101.65Z" />
    <path class="cls-4" d="M2877,2377.46c0,209.25-420.37,512-840.86,524.16-461.31,13.34-910.31-319.75-910.31-497.75q0-12.16.85-23.59c25.71,181.25,461.62,494.28,909.11,481.34,410.88-11.88,821.64-301.22,840.2-509.67Q2877,2364.22,2877,2377.46Z" />
    <path class="cls-4" d="M1730.65,3184.68a54.77,54.77,0,0,1-51.13,6.84c-16.08-6-29-18.39-38.52-36.88a116,116,0,0,1-11.55-36.67c-2.74-20.52,1-39.29,10.41-54.29-.07-.45-.14-.89-.19-1.35-7.55-63.51,14.39-136.94,61.84-207.16q9.77,2.91,19.62,5.67c-40.3,65.3-59.68,119-52.7,177.78,0,.46-.06-.45,0,0-9.41,15-11.81,48.57-9.07,69.09a116.08,116.08,0,0,0,11.55,36.67c9.48,18.49,22.44,30.9,38.52,36.88a54.69,54.69,0,0,0,19.13,3.46C1729.26,3184.72,1730,3184.71,1730.65,3184.68Z" />
    <path class="cls-4" d="M2377.29,3113.47c-3.09,26.72-17.73,62.24-48.78,74.41a54.79,54.79,0,0,1-51.25-6c.38,0,.77,0,1.15,0a55,55,0,0,0,20-3.8c31.05-12.18,45.68-47.7,48.78-74.42,2.38-20.56-.59-54.27-10.26-69.11.06-.44,0,.46,0,0,5.81-57.38-13.43-108.74-52.78-171.85q10-2.79,20-5.76c46.28,67.88,68.24,138.92,62,201-.05.46-.11.9-.17,1.35C2375.61,3074.2,2379.68,3092.91,2377.29,3113.47Z" />
    <path class="cls-4" d="M1985.53,3389.64a54.89,54.89,0,0,1-69.38,34.82c-26.37-8.75-93.26-15.62-132.68,1.84a54.92,54.92,0,0,1-77-46.67,54.85,54.85,0,0,0,71.84,26.72c39.42-17.46,106.32-10.6,132.69-1.85a54.9,54.9,0,0,0,72.06-55.64A54.7,54.7,0,0,1,1985.53,3389.64Z" />
    <path class="cls-4" d="M2298.58,3378a54.86,54.86,0,0,1-76.19,48c-39.72-16.76-106.49-8.73-132.71.47a54.89,54.89,0,0,1-68.2-74.4,54.91,54.91,0,0,0,73,54.36c26.22-9.21,93-17.24,132.7-.47a54.83,54.83,0,0,0,71.37-28Z" />
    <path class="cls-4" d="M2138.19,3086.24c-46.08,57.6-126.38,59.74-136.76,59.84-10.83.12-89.18-.38-134.63-57-21.19-26.39-28.4-57-27.06-94.28,1.63,28.47,9.6,52.76,26.88,74.28,45.46,56.63,123.81,57.13,134.63,57,10.38-.11,90.69-2.25,136.77-59.85,19.78-24.72,28.06-52.83,29.13-85.79C2169.07,3022.24,2161.85,3056.66,2138.19,3086.24Z" />
    <path class="cls-2" d="M2750.8,1816.91c-15.74-308.51-265-613.11-481.5-613.11-155.64,0-248.88,116.58-268,116.75-22.73.2-95.64-116.75-268-116.75-218.18,0-471.06,310.31-482.1,621.74q-.78-13.72-.81-28.48c-.26-318.49,259.27-643.25,482.47-643.26,172.41,0,245.31,117,268,116.75,19.15-.17,112.39-116.75,268-116.75,223.52,0,482,324.77,482.27,643.26Q2751.16,1807.25,2750.8,1816.91Z" />
    <path class="cls-2" d="M2473.67,674.35c-.15-17.72,8.93-28.23,62.15-5.43-69.23-42.34-129.15-60.54-128.88-29.37.57,65.4,174.11,150.61,135.25,190.15-47.82,48.66-143.94-40.34-539.76-11.2-533.75,39.29-964.77,540.12-966.09,1028.56-.35,129.83,38.54,280.85,111.58,425.48v0l1.29,2.52q1.86,3.66,3.74,7.3c.87-1.71,1.76-3.39,2.66-5.06l.66-1.21q2.73-5,5.66-9.77l.66-1.08q1.44-2.33,2.93-4.61l0-.08c1-1.56,2-3.09,3.1-4.61l.69-1q1.5-2.16,3-4.26l.15-.21q1.6-2.19,3.25-4.33c.25-.33.5-.66.76-1q1.5-1.95,3-3.85l.31-.39q1.65-2,3.33-4l.9-1q1.44-1.68,2.89-3.31l.61-.69c70.21-78.43,175.19-95.5,175.07-109.27,0-3.27-2.87-7.43-7.63-12.9-25.54,13.92-113.62,31-172.71,99.27-46.31-115.84-85.59-250.75-85.31-351.82,1.33-488.44,452-960.07,985.73-999.36,395.82-29.14,491.95,59.86,539.77,11.2C2661.39,829.46,2474.24,739.75,2473.67,674.35Z" />
    <path class="cls-2" d="M2966.2,1847.06c1.35-482.72-270.46-754.2-640.08-890.61-86.12-31.59-74.29-16.76-109.17-28.08,398.15,129.18,710.58,402,709.16,909-.32,115-27.37,241.26-86.32,372.62-57.8-80.49-163.52-88.39-192.17-100.84-4.72,5.46-7.52,9.79-7.49,13.56.13,15.13,121.52,20.18,190.45,107h0q5.36,6.73,10.25,14.14l0,0c1.08,1.63,2.13,3.28,3.17,5l0,.08c1,1.68,2.05,3.38,3,5.1a.41.41,0,0,1,.05.09q1.5,2.6,2.93,5.26l0,.07c1,1.79,1.89,3.6,2.81,5.45l0,0q1.38,2.77,2.7,5.64c1.5-3,3-6.05,4.48-9.09h0C2929.62,2119.3,2965.85,1972.14,2966.2,1847.06Z" />
    <path class="cls-2" d="M1949.22,3291.58c-.57,4.42-1.08,8.93-1.52,13.56-50.19-13.56-92.55-45.85-124.81-85.56q1.26-5.46,2.59-10.73C1857.73,3247.49,1899.7,3278.64,1949.22,3291.58Z" />
    <path class="cls-2" d="M2178.62,3217.15h0c-31.79,39.62-73.27,72.47-123,86.94q-.84-6.9-1.87-13.49c49.09-13.81,90.24-45.54,122-84.13Q2177.25,3211.71,2178.62,3217.15Z" />
    <path class="cls-2" d="M2194,3308c-52.67-5-105.6,3.5-136.57,13.54q-.4-4.95-.91-9.74c30.69-10.1,83.68-18.74,136.52-13.88Q2193.55,3302.92,2194,3308Z" />
    <path class="cls-2" d="M1947.33,3309.2c-.28,3.2-.52,6.45-.74,9.75-31.15-9.5-84.22-17.07-136.78-11.17.23-3.4.49-6.77.78-10.09C1863.34,3291.92,1916.47,3299.63,1947.33,3309.2Z" />
    <path class="cls-1" d="M1512.69,2523.35l-4.87-.57h0q-42.43-4.94-84.78-10.74c-32.77-40.15-56-82.73-67.74-125.83-8.9-32.68,14.68-60.05,47.08-55.44,5.22.73,10.43,1.47,15.65,2.19l2.44.33C1439.87,2399.23,1469.68,2463.73,1512.69,2523.35Z" />
    <path class="cls-1" d="M1740.15,2712.92a810.14,810.14,0,0,1-76.37-28.11c-88-37.57-162.38-88.33-216.88-145.75q40.57,5.33,81.21,9.86l1.48.16C1582.24,2611.78,1651.21,2667.87,1740.15,2712.92Z" />
    <path class="cls-1" d="M2647.24,2386.21c-11.75,43.09-35,85.68-67.74,125.83q-36.18,5-72.45,9.29l-11.58,1.37c41.19-59.77,69.41-124,87.46-189.53l3.16-.43,10.13-1.42,3.94-.55C2632.55,2326.16,2656.14,2353.53,2647.24,2386.21Z" />
    <path class="cls-1" d="M2555.63,2539.06c-48.1,50.68-111.69,96.17-186.44,132a790.65,790.65,0,0,1-74.71,31.12c75.86-43.85,135.64-96,182.09-153.34l1.48-.19,7.14-.93,1.67-.19Q2521.26,2543.58,2555.63,2539.06Z" />
    <path class="cls-2" d="M2304.15,2857q-39.86,11.91-80.78,20.95-8.65,1.92-17.35,3.68-25.77,5.23-51.81,9.2a972.49,972.49,0,0,1-118.06,10.79,1000.28,1000.28,0,0,1-179.37-11.42q-29-4.41-57.48-10.33c-3-.62-6-1.26-9-1.9q-45.07-9.72-88.76-22.8-8.44,12.48-15.79,25.09c30.82,9.66,62.31,18.19,94.3,25.39l.74-1.09,1.46-2.1c-.38,1.15-.75,2.3-1.11,3.43q34.26,7.68,69.24,13.27a1002.45,1002.45,0,0,0,186.06,12.46A975.24,975.24,0,0,0,2159.79,2920q33.12-5.21,65.84-12.5c1.77-.39,3.54-.78,5.3-1.19q45.21-10.26,89.19-24.13Q2312.7,2869.55,2304.15,2857Z" />
</g>
<g id="outline_shader" data-name="outline+shader">
    <path class="cls-2" d="M2214.79,2101.6c0,74.18-105.25,164.48-213.18,165.42-117.12,1-215.68-90.36-215.68-165.63q0-1.65.09-3.27c0-.3.06-.59.08-.89.06-.78.12-1.55.21-2.32,0-.35.1-.69.15-1s0-.41.08-.62c9.52,72.67,103.87,154.73,214.89,153.76,102.62-.89,202.81-82.57,212.43-154.36,0,.13,0,.26.08.39s0,.28.07.41a44.2,44.2,0,0,1,.66,5.1l0,.5C2214.75,2099.9,2214.79,2100.75,2214.79,2101.6Z" />
</g>

在上一个问题中,我询问了样式.我们将cls设置为1-4.我想输入5,以便cls从6到9开始.

In the previous question I asked about the style. in the style we have cls 1 - 4. I wanted to get an input say 5 so that in the style the cls will start from 6 until 9.

现在,我需要路径中的每个类标记也要更改. cls-1到cls-6 ... cls-4到cls-9等. 怎么能这样呢?我是否需要遍历XML中的所有路径? 谢谢.

Now I need that each class tag in the path will change as well. cls-1 to cls-6... cls-4 to cls-9 etc. How can one do so? Would I need to iterate over all path in the XML? Thanks.

推荐答案

使用LXML python模块解析SVG文件.然后,您可以快速轻松地访问所有节点和属性.我一直都在处理XML,HTML和SVG.蛮力字符串解析几乎永远不是正确的方法,尤其是在处理格式良好的语言语法(例如XML,SVG和CSS)时.

Use LXML python module to parse the SVG file. Then you can access all of the nodes and attributes quickly and easily. I do this all the time for XML, HTML, and SVG. Brute force string parsing is almost never the right way to go especially when dealing with a well formed language syntax like XML, SVG, and CSS.

我正在用手机打字,所以现在无法为您提供任何示例.但是,请放心,LXML使用简单,并为您提供了可靠的解决方案.健壮,与您一起破解的任何字符串解析代码都不一样.

I'm typing on my phone so I can't provide you with any examples right now. However, rest assured that LXML is simple to use and gives you a robust solution; robust unlike any string parsing code that you hack together.

使用XPath,您可以轻松找到所有节点并修改其属性.就您而言:

Using XPath, you can easily find all of the nodes and modify their attributes. In your case:

for node in doc.xpath ("//path"):
    node.get ("class")

这篇关于在SVG Python中更改路径类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆