浏览器html中加入word,撸个插件给你word-to-html

ac6e138cd40d0ee7c6c5bb1723a3ce9c.png

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

d076513e186cd5ba84341f4d0411fdaf.png

emmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm!

安利一波其他项目:

时间仓促,代码写得有点乱,这里贴一下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

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 be fixed 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,env])

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

env: 'node' | 'browser'

default value is 'node',if you want to convert the font-family/font -size/color, env must be 'browser', then you get a .html file, after you open the .html file in chrome, you will get a string in console panel, the result is what you want.

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

step 1: execute the code below in your node

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'},'browser')复制代码

step 2: get the html string in your browser

open the html file generated just now, and copy the result string of the console panel into your html tempalte, you will see the content of your .docx file will be in your html template.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值