我在python3中使用BeautifulSoup模块来修改我用Inkscape创建的一些svg文件。具体来说,我正在修改这些文件中的一些文本,并在某些情况下更改某些对象的颜色。我注意到,无论我做什么,所有文本的位置总是在输出svg文件中移动。 例如参见:svg in/out files + png versions为什么使用BeautifulSoup修改SVG文件时文本对象发生了变化?
这似乎是发生的是文本对象的大小是相对于原来的文件输出SVG不同。我可以将文本对象从输出文件复制到原始文件,我不再看到这种转变,但这是一个恼人的解决方案。
有没有人知道是什么原因导致文本对象大小发生这种变化,并且可以防止这种变化?
下面的代码的一个例子位我已经运行(输入和输出svgs的拷贝是在上面的链接):
from bs4 import BeautifulSoup
svgFile = "test_in.svg"
outputFile = "test_out.svg"
svg = open(svgFile, 'r').read()
soup = BeautifulSoup(svg, features = 'xml')
texts = soup.findAll('text')
for t in texts:
if t['id'] == 'testID':
print(t, '\n')
t.contents[0].string = 'new text'
print(t, '\n')
# Output the edited SVG file
f = open(outputFile, "w")
f.write(soup.prettify())
f.close()
虽然目前没有出现任何变化中的文本元素xml/svg树,所以我觉得问题必须从文件的其他部分进行更改。 (另外,我可以省略t.contents[0].string = 'new text'
和文本运动仍会发生。)第一print(t)
给出:
<text id="testID" inkscape:label="#text3581" sodipodi:linespacing="125%" style="font-size:20px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Arial;-inkscape-font-specification:Arial" transform="matrix(0,1,-1,0,0,0)" x="449.63721" xml:space="preserve" y="-280.92737"><tspan id="tspan3583" sodipodi:role="line" style="font-size:14px;font-weight:normal;-inkscape-font-specification:Arial" x="449.63721" y="-280.92737">Text to change</tspan></text>
第二打印(t)的输出看起来是完全一样的,不同之处'text to change'
现在是'new text'
:
<text id="testID" inkscape:label="#text3581" sodipodi:linespacing="125%" style="font-size:20px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Arial;-inkscape-font-specification:Arial" transform="matrix(0,1,-1,0,0,0)" x="449.63721" xml:space="preserve" y="-280.92737"><tspan id="tspan3583" sodipodi:role="line" style="font-size:14px;font-weight:normal;-inkscape-font-specification:Arial" x="449.63721" y="-280.92737">new text</tspan></text>
任何见解将不胜感激!
请发布一个文本对象的小svg示例,在您的转换过程中移动。 –
编辑包括png版本的输入和输出svg文件 – Gingerbeardman