首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Nodejs从docx文件中提取文本

如何使用Nodejs从docx文件中提取文本
EN

Stack Overflow用户
提问于 2017-07-06 23:20:05
回答 3查看 5.3K关注 0票数 1

我想从docx文件中提取文本,我已经尝试使用mammoth

代码语言:javascript
复制
var mammoth = require("mammoth");
mammoth.extractRawText({path: "./doc.docx"})
    .then(function(result){
        var text = result.value; // The raw text 

        //this prints all the data of docx file
        console.log(text);

        for (var i = 0; i < text.length; i++) {
            //this prints all the data char by char in separate lines
            console.log(text[i]);
        }
        var messages = result.messages;
    })
    .done();

但是这里的问题是,在这个for循环中,我想要逐行的数据,而不是逐字符的,请帮助我,或者有其他你知道的方法吗?

EN

回答 3

Stack Overflow用户

发布于 2018-01-16 21:32:37

一种方法是获取整个文本,然后按'\n'拆分:

代码语言:javascript
复制
import superagent from 'superagent';
import mammoth from 'mammoth';

const url = 'http://www.ojk.ee/sites/default/files/respondus-docx-sample-file_0.docx';

const main = async () => {

  const response = await superagent.get(url)
    .parse(superagent.parse.image)
    .buffer();

  const buffer = response.body;

  const text = (await mammoth.extractRawText({ buffer })).value;
  const lines = text.split('\n');

  console.log(lines);
};

main().catch(error => console.error(error));
票数 1
EN

Stack Overflow用户

发布于 2020-07-23 16:01:52

您可以使用any-text

用法很简单:

代码语言:javascript
复制
var reader = require('any-text');

reader.getText(`path-to-file`).then(function (data) {
  console.log(data);
});
票数 1
EN

Stack Overflow用户

发布于 2020-01-03 13:41:04

代码语言:javascript
复制
    var mammoth = require("mammoth");
var path = require("path");

var filePath = path.join(__dirname,'./doc.docx');

mammoth.extractRawText({path: filePath})
    .then(function(result){
        var text = result.value; // The raw text

        //this prints all the data of docx file
        //console.log(text);
        console.log('------------------------------');
        var textLines = text.split ("\n");

        for (var i = 0; i < textLines.length; i++) {
            //this prints all the data in separate lines
            console.log(textLines[i]);
        }
        var messages = result.messages;
    })
    .done();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44953008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档