一、背景
因业务需求,目前正在实现一项需求,即将一份试卷的内容提取出来,由非结构化到结构化的转换。其中在对数学试卷进行解析的时候发现试卷中有数学公式,想要把数学公式也要提取出来,并且展示到web页面的时候遇到了困难,以前也没有遇到过,花了一番功夫,最终还是搞定了,于是写下来分享给大家。
二、概述
从一份word格式的数学试卷中,用java提取公式,此刻数据结构是OMML格式,接下来需要转换为mathml格式,最后再转换为latex格式,将latex格式推给前端由前端进行渲染。
三、正文
1、使用java解析word文件,输出数学公式OMML格式内容。
下图是word内容:

Java解析文件的代码:
public static void readMathFormulas2(String filePath) throws IOException {
// 创建文件输入流
FileInputStream fis = new FileInputStream(filePath);
// 创建XWPFDocument对象
XWPFDocument document = new XWPFDocument(fis);
for (IBodyElement element : document.getBodyElements()) {
if (element instanceof XWPFParagraph) {
XWPFParagraph paragraph = (XWPFParagraph) element;
// 处理块级公式
for (CTOMath math : paragraph.getCTP().getOMathList()) {
String omml = math.xmlText();
System.out.println("Block formula: " + omml);
}
// 处理内联公式
for (CTOMathPara math22 : paragraph.getCTP().getOMathParaList()) {
String omml = math22.xmlText();
System.out.println("Inline formula: " + omml);
}
}
}
}
解析出来的数学公式OMML格式内容:
<m:oMath xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.mi

最低0.47元/天 解锁文章
744

被折叠的 条评论
为什么被折叠?



