我想从docx文件中提取文本,我已经尝试使用mammoth
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循环中,我想要逐行的数据,而不是逐字符的,请帮助我,或者有其他你知道的方法吗?
发布于 2018-01-16 21:32:37
一种方法是获取整个文本,然后按'\n'拆分:
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));发布于 2020-07-23 16:01:52
您可以使用any-text
用法很简单:
var reader = require('any-text');
reader.getText(`path-to-file`).then(function (data) {
console.log(data);
});发布于 2020-01-03 13:41:04
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();https://stackoverflow.com/questions/44953008
复制相似问题