撸个插件给你word-to-html

撸个插件给你word-to-html_第1张图片

最近遇到一个需求,需要将非常多内容的(文字/表格)word文档展示出来,这个需求出现在pc端就用插件好了或者直接下载文件?如果需求是在移动端呢?怎么办?转成html吧。。。几十页的word怎么搞?为了造福大家,花了几天时间撸了一个插件word-to-html,可以转嵌套的表格,合并单元格的表格,github地址.

撸个插件给你word-to-html_第2张图片

emmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm!
安利一波其他项目:

NodeNote,持续更新中react相关库源码浅析
react ts3 项目


时间仓促,代码写得有点乱,这里贴一下reamde吧,谁用谁知道哈,如果解决给位的痛点,希望不要吝啬您的star,非常欢迎提issue,大家一起讨论完善。由于用到了jsdom,这个库模拟出来的DOMpaser有点弱,如果你选择在浏览器中用我的给的方法,你甚至能将word中每一行不同文字的字体字号都转成对应的html,借助浏览器的js调试面板的源码我放在了github上对应项目的test/browser文件夹中了。

下面是readme:

word-to-html

A tiny tool to convert Microsoft Word document to HTML in Nodejs and in chrome,
you can use the tool convert tables with merged cells and nested tables to html file in Nodejs or chrome, the online tool wordhtml can not do this.
Beyond that, you can convert words with different font-family or font-size in a line to html string in chrome.

table example

example.html(mobile)

attention

If a line of words have different font-family or font-size in your .docx, it can not convert
your .docx to html expectly in nodejs, but this can fix in the browsers such as chrome. because
the npm package jsdom can not realize the DOMParser's function perfectly.
So if you want to convert the font-family and font-size exactly, you can see how to use word2html.js in browsers!

Install

npm i word-to-html --save-dev

or

yarn add word-to-html

api in nodejs: word2html(absPath [,options])

absPath: string | Array

absPath is your file's absolute path

options: {tdTextAlign:string,tdVerticalAlign:string}

tdTextAlign controls the tag's text-align

tdVerticalAlign controls the tag's vertical-align

Usage in nodejs

var path = require('path');
var word2html = require('word-to-html');
//Word document's absolute path
var absPath = path.join(__dirname,'test.docx');
word2html(absPath,{tdVerticalAlign:'top'})

the html generated in your WorkSpace.

Usage in browsers

details in my github

step 1: Take the code in your html or your console panel as the global functions

step 2: Use the code to convert your .docx to xml sting in your .xml file

var admZip = require('adm-zip');
var fs = require('fs');
var path = require('path');
var resultList = [];
const tableName = [
    'test.docx' // replace your docx name here
];
tableName.forEach((item, index)=>{
    var absPath = path.join(__dirname,item);
    fs.exists(absPath, function(exists){
        if(exists){
            const zip = new admZip(absPath);
            var contentXml = zip.readAsText("word/document.xml");
            var len = item.length-1;
            var name = item.slice(0,len-4) + ".xml"
            fs.writeFileSync(name,contentXml)           
        }else{
            callback(resultList)
        }
    })
});

step 3: Take your .xml file's string in variables str, and excute the code in your browser. the res is your html string, you can put it to the template html.

var str =`${your xml sting}`
var res = convert(loadXML(str));

step4:template example

html-body:

{res}

head:


    

你可能感兴趣的:(javascript,html,word,转换)