富在术数不在劳身,利在局势不在力耕
import xml.etree.ElementTree as ET
import cairosvg
# 解析SVG文件
tree = ET.parse('your_svg_file.svg')
root = tree.getroot()
# 修改颜色
# 查找<style>标签并替换颜色
for style in root.findall('{http://www.w3.org/2000/svg}style'):
if '.st0{fill:#E61D62;}' in style.text:
style.text = style.text.replace('#E61D62', 'your_new_color') # 替换为新颜色
# 添加文字
namespace = {'svg': 'http://www.w3.org/2000/svg'}
ET.register_namespace('', "http://www.w3.org/2000/svg")
text = ET.SubElement(root, '{http://www.w3.org/2000/svg}text', attrib={
'x': '50', # 文字的X坐标
'y': '50', # 文字的Y坐标
'font-size': '20', # 字体大小
'fill': 'blue' # 文字颜色
})
text.text = 'Your Text Here' # 添加的文字内容
# 将修改后的SVG保存为PNG
svg_data = ET.tostring(root, encoding='unicode')
cairosvg.svg2png(bytestring=svg_data.encode('utf-8'), write_to='your_output_png_file.png')
文章分类