2017-07-06 37 views
0

でのdocxファイルからテキストを抽出する方法、私はマンモス私はDOCXファイルからテキストを抽出したいNodejs

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ループでは、私が代わりにデータを1行ずつ欲しいということですcharのcharの、ここで私を助けてくださいまたはあなたが知っている他の方法がありますか?

+0

あなたが行ずつ意味していますか? word文書の個々の行や改行で区切られた段落と同様? –

+0

@ExplosionPillsの個々の行が好きです – iwayankit

+0

"\ n"であなたのテキストを分割しています! – tashakori

答えて

0

一つの方法は、テキスト全体を取得することであり、その後、'\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)); 
関連する問題