这应该很简单,所以不确定为什么我在标题中收到错误。创建的SVG很好,因为我手动检查了
code to generate svg
....
mod_svg = 'final1.svg'
tree.write(mod_svg)
import cairosvg
fileout = open('finaltree','w')
cairosvg.svg2png(bytestring=mod_svg,write_to=fout)
fileout.close()
我在pythonany上使用python 2.7,以防万一。
完全追溯
Traceback (most recent call last):
File "/home/drdavepier/mysite/flask_app.py", line 184, in <module>
cairosvg.svg2png(bytestring=mod_svg,write_to=fout)
File "/usr/local/lib/python2.7/dist-packages/cairosvg/__init__.py", line 43, in <lambda>
surface_type.convert(*args, **kwargs))(_surface_type)
File "/usr/local/lib/python2.7/dist-packages/cairosvg/surface/__init__.py", line 82, in convert
tree = Tree(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/cairosvg/parser.py", line 270, in __init__
tree = ElementTree.fromstring(bytestring)
File "lxml.etree.pyx", line 3032, in lxml.etree.fromstring (src/lxml/lxml.etree.c:68121)
File "parser.pxi", line 1786, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:102470)
File "parser.pxi", line 1674, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:101299)
File "parser.pxi", line 1074, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:96481)
File "parser.pxi", line 582, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:91290)
File "parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:92476)
File "parser.pxi", line 622, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:91772)
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1
打开相关SVG的行
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (http://matplotlib.org/) --><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="432pt" version="1.1" viewBox="0 0 576 432" width="576pt">
树的初始化:
filename = "/path/to/file.svg"
with open( filename, 'r') as infile:
tree = etree.parse( infile )