nodejs将word文档转为html文件(脚本)--获取word文档内容

来源:互联网 发布:手机基站定位软件 编辑:程序博客网 时间:2024/05/14 10:49

本来是想写 用nodejs 或者 js 将word文档转为html的脚本,
js在IE浏览器下有 new ActiveApplication 对象可以 获取文档
nodejs 获取文档看这篇http://www.jianshu.com/p/68a420a68ded 因为还没时间整理 所以先给大家个链接

思路是这样,先获取文档内容, 可以用代码得到内容也就是跟我们通过接口获取服务端的数据一样了,之后变可以 用js 拼接出一个html 文件


  let fs = require("node-fs")  let AdmZip = require('adm-zip')  let zip = new AdmZip('./help.zip')  let contentXml = zip.readAsText("word/document.xml")  let str = ""  let arr = []  let header = "<!DOCTYPE html>\n" +    "<html lang=\"en\">\n" +    "<head>\n" +    "  <meta charset=\"UTF-8\">\n" +    "  <title>Title</title>\n" +    "</head>\n" +    "<body>"  let footer = "</body>\n" +    "</html>"  let one = ''  // console.log('111',contentXml)  // console.log('111',contentXml.match(/<w:t>[\S]*?<\/w:t>/ig))  // return  // contentXml.match(/<w:t xml:space="preserve">[\S]*?<\/w:t>/ig).forEach((item,index,array) => {  contentXml.match(/<w:t>[\S]*?<\/w:t>/ig).forEach((item,index,array) => {    // console.log(index+':'+'刚进入循环的str:',str)    // let tem = item.slice(26, -6)    let tem = item.slice(5, -6)    // console.log(array[index].slice(26, -6),array[index].slice(26, -6).length)    // return    // console.log(index,':',tem)    if (tem && tem !== "" && tem !== " " && tem !== "  " && tem !== "   ") {      if (tem.length === 1) {        // console.log('tem为1的时候的tem:',tem)        // str += tem        if (str !== '') {          // console.log('str不为空的时候的str',str)          one = tem          // str = str.slice(0,-5) + tem + '</p>'        } else {          // console.log('str空的时候')        }      } else {        // console.log('tem非1的时候的tem',tem)        str +='<p>' + one + tem + '</p>' + "\n"        // console.log('tem非1的时候的str',str)        one = ''      }    }    // console.log(index+':'+'出循环的str',str)  })// return  str = header + "\n" + str + "\n" + footer  console.log('我要输出了')  fs.writeFile("./help.html",str,(err) => {    console.log('111',err)  })returnif (!arr[1]) {  arr.splice(1, 1);}arr.forEach((item, index, arr) => {  if (!item || item === ' ') {    arr.splice(index, 1)  }})arr.forEach((item, index, array) => {  if (item.length === 1) {    array[index - 1] = array[index - 1] + item  }})fs.writeFile("./documentXml.txt", contentXml, (err) => {  console.log(err)})fs.writeFile("./numbering.txt", numberingXml, (err) => {  console.log(err)})fs.writeFile("./styles.txt", stylesXML, (err) => {  console.log(err)})// str += '<p>' + item.slice(26, -6) + '</p>'// arr.push(item.slice(26, -6))
原创粉丝点击