mammoth定制开发,提取word(docx)中的公式(OMath格式)

效果图展示

效果展示
效果展示

需求说明

  1. 需要提取docx中的内容,经过工作流处理之后,重新生成文档
  2. 要求支持公式,表格样式,图片大小还原到新的文档中

参考文章/网站

  1. 魔改mammoth支持导入样式
  2. docx文档
  3. 在线latex公式编辑器
  4. MathML

方案选择

由于之前使用过mammoth,并且实现过mammoth的定制开发,因此决定使用mammoth.js和docx.js来实现这个功能

方案流程

  1. mammoth提取docx内容转换成html
  2. html实现还原docx中的样式并转换成docx.js能支持的数据格式
  3. 还原docx

本文主要是实现公式的提取

实现方法

  1. 通过查看docx原始的xml(通过更改docx文件拓展名,改为zip之后解压缩),获取公式的各种标签
  2. 定制开发解析的方法
  3. 解析对象转换成html能显示的MathML

xml标签示例

xml标签

关键代码

document.js

function OMath(children) {
   
   
    return {
   
   
        type: types.math,
        children: children
    };
}

function OMathText(value) {
   
   
    return {
   
   
        type: types.mathText,
        value: value
    };
}

function OMathFraction(numerator, denominator) {
   
   
    return {
   
   
        type: types.mathFraction,
        numerator: numerator,
        denominator: denominator
    };
}

function OMathRadical(children, degree) {
   
   
    return {
   
   
        type: types.mathRadical,
        children: children,
        degree: degree
    };
}

function OMathNary(children, options, subScript, superScript){
   
   
    return {
   
   
        type: types.mathNary,
        children: children,
        options: options,
        subScript: subScript,
        superScript: superScript
    };
}
function OMathFunction(children, name) {
   
   
    return {
   
   
        type: types.mathFunction,
        children: children,
        name: name
    };
}
function OMathSuperScript(children, superScript) {
   
   
    return {
   
   
        type: types.mathSuperScript,
        children: children,
        superScript: superScript
    };
}
function OMathSubScript(children, subScript) {
   
   
    return {
   
   
        type: types.mathSubScript,
        children: children,
        subScript: subScript
    };
}
function OMathSubSuperScript(children, subScript, superScript) {
   
   
    return {
   
   
        type: types.mathSubSuperScript,
        children: children,
        subScript: subScript,
        superScript: superScript
    };
}
function OMathPreSubSuperScript(children, subScript, superScript) {
   
   
    return {
   
   
        type: types.mathPreSubSuperScript,
        children: children,
        subScript: subScript,
        superScript: superScript
    };
}
function OMathBracket(children, bracketType) {
   
   
    return {
   
   
        type: types.mathBracket,
        children: children,
        bracketType: bracketType
    };
}

function OMathLimitUpper(children, limit) {
   
   
    return {
   
   
        type: types.mathLimitUpper,
        children: children,
        limit: limit
    };
}
function OMathLimitLower(children, limit) {
   
   
    return {
   
   
        type: types.mathLimitLower,
        children: children,
        limit: limit
    };
}

body-reader.js

var xmlElementReaders = {
   
   
     _oMath: function(element) {
   
   
            return elementResult(new documents.OMath(readChildElements(element)));
        },
    // 对应children属性
        _e: readChildElements,
    // 处理公式内的元素转换
        _r: readChildElements,
        _t: function(element) {
   
   
            return elementResult(new documents.OMathText(element.text()));
        },
        _f: function(element) {
   
   
            return elementResult(
        new documents.OMathFraction(readChildElements(element.mathFirst('num')), readChildElements(element.mathFirst('den')))
      );
        },
        _num: readChildElements,
        _den: readChildElements,
        _rad: function(element) {
   
   
            return elementResult(
        new documents.OMathRadical(readChildElements(element.mathFirst('e')), readChildElements(element.mathFirst('deg')))
      );
        },
    // 对应开根号的指数
        _deg: readChildElements,
    // 积分运算
        _nary: function(element) {
   
   
            var narySub = element.mathFirst('sub');
            var narySup = element.mathFirst('sup');
            var naryPr = element.mathFirst('naryPr');
            var chr = naryPr.mathFirstOrEmpty('chr').attributes[mathVersion + 'val'] || '';
            var limLoc = naryPr.mathFirstOrEmpty('limLoc').attributes[mathVersion + 'val'] || '';
            var options = {
   
   
                chr: chr,
                limLoc: limLoc
            };
            var subScript = narySub ? readChildEleme
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值