get docx file contents using javascript/jquery

前端 未结 3 1917
一整个雨季
一整个雨季 2020-12-06 08:07

wish to open / read docx file using client side technologies (HTML/JS).

kindly assist if this is possible . have found a Javascript library named docx.js but person

3条回答
  •  心在旅途
    2020-12-06 08:35

    I know this is an old post, but doctemplater has moved on and the accepted answer no longer works. This worked for me:

    function loadDocx(filename) {
      // Read document.xml from docx document
      const AdmZip = require("adm-zip");
      const zip = new AdmZip(filename);
      const xml = zip.readAsText("word/document.xml");
      // Load xml DOM
      const cheerio = require('cheerio');
      $ = cheerio.load(xml, {
        normalizeWhitespace: true,
        xmlMode: true
      })
      // Extract text
      let out = new Array()
      $('w\\:t').each((i, el) => {
        out.push($(el).text())
      })
      return out
    }
    

提交回复
热议问题