C#实现批量替换SVG中transform属性为x/y属性的方法
如何批量替换SVG中text元素的translate变换为x/y属性
你需要将SVG字符串里text元素的transform="translate(X,Y)"替换为x="X" y="Y",下面是两种可行的实现方案:
方案一:正则表达式(适合简单场景)
如果你的SVG里text元素的transform属性只有translate(X,Y)这一种形式,没有其他复合变换(比如同时包含rotate、scale),可以用正则捕获组来提取X和Y的值,直接替换字符串。
Python示例
import re svg_str = '''<svg xmlns="http://www.w3.org/2000/svg" width="475.07" height="475.07" viewBox="-5 -5 1200.075 900.075" fill="#FFF"> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(52.7996,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-800</text> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(117.0119,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-700</text> <polyline points="52.7996,45.8345 52.7996,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> <polyline points="117.0119,45.8345 117.0119,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> </svg>''' # 匹配transform="translate(X,Y)"的正则,捕获X和Y pattern = re.compile(r'transform="translate\(([^,]+),([^)]+)\)"') # 替换为x="X" y="Y" modified_svg = pattern.sub(r'x="\1" y="\2"', svg_str) print(modified_svg)
JavaScript示例
const svgStr = `<svg xmlns="http://www.w3.org/2000/svg" width="475.07" height="475.07" viewBox="-5 -5 1200.075 900.075" fill="#FFF"> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(52.7996,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-800</text> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(117.0119,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-700</text> <polyline points="52.7996,45.8345 52.7996,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> <polyline points="117.0119,45.8345 117.0119,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> </svg>`; const regex = /transform="translate\(([^,]+),([^)]+)\)"/g; const modifiedSvg = svgStr.replace(regex, 'x="$1" y="$2"'); console.log(modifiedSvg);
方案二:使用SVG/XML解析库(更可靠,推荐)
如果你的SVG可能存在复杂的transform组合(比如translate+rotate),或者属性格式不固定(比如空格、换行),用专门的解析库能更准确地处理,避免正则匹配出错。
Python示例(使用lxml)
from lxml import etree svg_str = '''<svg xmlns="http://www.w3.org/2000/svg" width="475.07" height="475.07" viewBox="-5 -5 1200.075 900.075" fill="#FFF"> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(52.7996,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-800</text> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(117.0119,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-700</text> <polyline points="52.7996,45.8345 52.7996,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> <polyline points="117.0119,45.8345 117.0119,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> </svg>''' # 解析SVG字符串 root = etree.fromstring(svg_str.replace('"', '"')) # 遍历所有text元素 for text_elem in root.xpath('//text'): transform_attr = text_elem.get('transform') if transform_attr and 'translate' in transform_attr: # 提取translate的参数 translate_part = transform_attr.split('(')[1].split(')')[0] x, y = translate_part.split(',') # 添加x和y属性 text_elem.set('x', x.strip()) text_elem.set('y', y.strip()) # 删除transform属性 text_elem.attrib.pop('transform') # 转换回字符串,替换引号为" modified_svg = etree.tostring(root, encoding='unicode').replace('"', '"') print(modified_svg)
JavaScript示例(使用DOMParser)
const svgStr = `<svg xmlns="http://www.w3.org/2000/svg" width="475.07" height="475.07" viewBox="-5 -5 1200.075 900.075" fill="#FFF"> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(52.7996,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-800</text> <text dominant-baseline="hanging" text-anchor="middle" transform="translate(117.0119,761.494)" font-family="Segoe UI" font-size="12" font-weight="400" fill="black">-700</text> <polyline points="52.7996,45.8345 52.7996,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> <polyline points="117.0119,45.8345 117.0119,750.494" style="fill:none;stroke:rgb(0,0,0);stroke-width:1;stroke-opacity:0.250980392156863" shape-rendering="crispEdges" /> </svg>`; // 解析SVG const parser = new DOMParser(); const doc = parser.parseFromString(svgStr.replace('"', '"'), 'image/svg+xml'); // 获取所有text元素 const textElements = doc.querySelectorAll('text'); textElements.forEach(elem => { const transform = elem.getAttribute('transform'); if (transform && transform.includes('translate')) { // 提取X和Y const match = transform.match(/translate\(([^,]+),([^)]+)\)/); if (match) { const x = match[1].trim(); const y = match[2].trim(); elem.setAttribute('x', x); elem.setAttribute('y', y); elem.removeAttribute('transform'); } } }); // 转换回字符串,替换引号为" const modifiedSvg = new XMLSerializer().serializeToString(doc).replace('"', '"'); console.log(modifiedSvg);
内容的提问来源于stack exchange,提问作者Lluis
相关产品推荐
相关产品推荐

