這篇文章主要給大家介紹了關于linux系統(tǒng)中利用node.js提取word(doc/docx)及pdf文本的內(nèi)容,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面跟著小編來一起看看吧。
前言
想要做全文搜索引擎,則需要將word/pdf等文檔內(nèi)容提取出來。對于pdf有xpdf等一些開源方案。
但Word文檔的情況則會復雜一些。
提取PDF文本內(nèi)容
XPDF是一個免費開源的軟件,用于顯示PDF文件,并可將pdf轉(zhuǎn)換成文字圖片等,同樣支持Windows版。在Debian linux上安裝非常簡單:
apt-get?install?xpdf
我們這里只使用pdftotext這個功能,直接輸入可查看幫助:
root@raspberrypi:/var/www#?pdftotext pdftotext?version?0.26.5 Copyright?2005-2014?The?Poppler?Developers?-?http://poppler.freedesktop.org Copyright?1996-2011?Glyph?&?Cog,?LLC Usage:?pdftotext?[options]?<pdf-file>?[<text-file>] ?-f?<int>???:?first?page?to?convert ?-l?<int>???:?last?page?to?convert ?-r?<fp>???:?resolution,?in?DPI?(default?is?72) ?-x?<int>???:?x-coordinate?of?the?crop?area?top?left?corner ?-y?<int>???:?y-coordinate?of?the?crop?area?top?left?corner ?-W?<int>???:?width?of?crop?area?in?pixels?(default?is?0) ?-H?<int>???:?height?of?crop?area?in?pixels?(default?is?0) ?-layout???:?maintain?original?physical?layout ?-fixed?<fp>??:?assume?fixed-pitch?(or?tabular)?text ?-raw????:?keep?strings?in?content?stream?order ?-htmlmeta???:?generate?a?simple?HTML?file,?including?the?meta?information ?-enc?<string>??:?output?text?encoding?name ?-listenc???:?list?available?encodings ?-eol?<string>??:?output?end-of-line?convention?(unix,?dos,?or?mac) ?-nopgbrk???:?don't?insert?page?breaks?between?pages ?-bbox????:?output?bounding?box?for?each?word?and?page?size?to?html.?Sets?-htmlmeta ?-opw?<string>??:?owner?password?(for?encrypted?files) ?-upw?<string>??:?user?password?(for?encrypted?files) ?-q????:?don't?print?any?messages?or?errors ?-v????:?print?copyright?and?version?info ?-h????:?print?usage?information ?-help????:?print?usage?information ?--help???:?print?usage?information ?-?????:?print?usage?information</string></string></string></string></fp></int></int></int></int></fp></int></int></text-file></pdf-file>
測試一下:
root@raspberrypi:/var/www#?pdftotext?onceai.pdf?onceai.txt root@raspberrypi:/var/www#?cat?onceai.txt?產(chǎn)品介紹?頑石智能科技(上海)有限公司 ....
然后在node.js中使用 child_process直接調(diào)用此命令即可,pdftotext會將內(nèi)容輸出以文本文件中,可能需要多一些操作。具體代碼略。
用antiword提取 .doc 的內(nèi)容
我們這里使用了 antiword 開源軟件,來提取word2003以前版本的內(nèi)容,安裝同樣非常簡單:
apt-get?install?antiword
查看幫助:
root@raspberrypi:/var/www#?antiword ?Name:?antiword ?Purpose:?Display?MS-Word?files ?Author:?(C)?1998-2005?Adri?van?Os ?Version:?0.37?(21?Oct?2005) ?Status:?GNU?General?Public?License ?Usage:?antiword?[switches]?wordfile1?[wordfile2?...] ?Switches:?[-f|-t|-a?papersize|-p?papersize|-x?dtd][-m?mapping][-w?#][-i?#][-Ls] ??-f?formatted?text?output ??-t?text?output?(default) ??-a?<paper>?Adobe?PDF?output ??-p?<paper>?PostScript?output ???paper?size?like:?a4,?letter?or?legal ??-x?<dtd>?XML?output ???like:?db?(DocBook) ??-m?<mapping>?character?mapping?file ??-w?<width>?in?characters?of?text?output ??-i?<level>?image?level?(PostScript?only) ??-L?use?landscape?mode?(PostScript?only) ??-r?Show?removed?text ??-s?Show?hidden?(by?Word)?text</level></width></mapping></dtd></paper></paper>
antiword直接將word內(nèi)容輸出到了console中:
root@raspberrypi:/var/www#?antiword?spec.doc SYNC?Mobile?–?Ford?APA Project?Number:?DFYST Requirements?Specification
同樣在node.js用child_process調(diào)用此命令即可。
解析提取.docx 的內(nèi)容
對于 docx 文檔來說,因基本身就是一個zip文件,只需要在node.js先將其解壓,再解析 text.docxwordnode.js.xml 文件即可。
總結
? 版權聲明
文章版權歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END