从sympy/svgmath生成SVG时出错

2024-09-28 03:19:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用sympy和svgmath将代数呈现为SVG。我有档案'svgmath.xml文件'在根目录中。不幸的是,我得到了:“ImportError:No module named libxml2”。我不确定最好的解决办法。如有任何帮助/建议,我将不胜感激。我的代码是:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(c2p(mathml(3*x), simple=False))

堆栈跟踪现在是:

^{pr2}$

我修改的代码:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

import libxml2

import StringIO

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(StringIO.StringIO(mathml(3*x)))

这将无法生成有效的SVG文件,并给出:

[WARNING] line 1, column 0: Root element in MathML document must be 'math'
[WARNING] line 1, column 7: MathML element 'times' is unsupported
[WARNING] line 1, column 15: MathML element 'cn' is unsupported
[WARNING] line 1, column 25: MathML element 'ci' is unsupported
[WARNING] line 1, column 0: MathML element 'apply' is unsupported

Tags: fromimportparserlinecolumnxmlelementmathml
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:11

安装libxml2库。例如,在ubuntu上,这是python-libxml2包。我也尝试过sympy->;mathml->;svg路由一次,还必须安装该包(iirc是c2p()所需的,它应用XSLT将内容转换为presentation mathml)。在

相关问题 更多 >

    热门问题