简体   繁体   English

如何用Python修改an.SVG文件的内容?

[英]How to modify the contents of an .SVG file with Python?

  1. I have a.svg file with example contents: <svg style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.001" /></svg>我有一个带有示例内容的.svg 文件: <svg style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.001" /></svg>

  2. I now want to use Python to directly edit the.svg file and change style="fill:#000000; to my desired color and save it., but I am not sure how to go about this, I have tried a lot of libraries but none do what I need.我现在想使用 Python 直接编辑 .svg 文件并将style="fill:#000000;更改为我想要的颜色并保存它。但是我不确定如何 Z34D1F91FB2E514B8576ZFAB1A但没有人做我需要的。

Try this: https://pythonexamples.org/python-replace-string-in-file/试试这个: https://pythonexamples.org/python-replace-string-in-file/

#read input file
fin = open("data.svg", "rt")

#read file contents to string
data = fin.read()

#replace all occurrences of the required string
data = data.replace('style="fill:#000000;', 'style="fill:#FF0000;')

#close the input file
fin.close()

#open the input file in write mode
fin = open("data.svg", "wt")

#overrite the input file with the resulting data
fin.write(data)

#close the file
fin.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM