Linux系統(tǒng)中利用node.js提取Word(doc/docx)及PDF文本的內(nèi)容
前言
想要做全文搜索引擎,則需要將word/pdf等文檔內(nèi)容提取出來(lái)。對(duì)于pdf有xpdf等一些開(kāi)源方案。
但Word文檔的情況則會(huì)復(fù)雜一些。
提取PDF文本內(nèi)容
XPDF是一個(gè)免費(fèi)開(kāi)源的軟件,用于顯示PDF文件,并可將pdf轉(zhuǎn)換成文字圖片等,同樣支持Windows版。在Debian Linux上安裝非常簡(jiǎn)單:
apt-get install xpdf
我們這里只使用pdftotext這個(gè)功能,直接輸入可查看幫助:
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
測(cè)試一下:
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會(huì)將內(nèi)容輸出以文本文件中,可能需要多一些操作。具體代碼略。
用antiword提取 .doc 的內(nèi)容
我們這里使用了 antiword 開(kāi)源軟件,來(lái)提取word2003以前版本的內(nèi)容,安裝同樣非常簡(jiǎn)單:
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 size name> Adobe PDF output -p <paper size name> 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
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)容
對(duì)于 docx 文檔來(lái)說(shuō),因基本身就是一個(gè)zip文件,只需要在node.js先將其解壓,再解析 text.docx\word\document.xml 文件即可。
Github上也有些將docx解析成html的庫(kù),
如:
https://github.com/mwilliamson/mammoth.js
https://github.com/lalalic/docx2html
等。
總結(jié)
以上就是這文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
windows系統(tǒng)上完全卸載并重裝Node的步驟(親測(cè)可用)
對(duì)于Windows平臺(tái)來(lái)說(shuō),所有的應(yīng)用程序,其安裝卸載都是一樣的,node.js也不例外,但是還是很多用戶不明白,下面這篇文章主要給大家介紹了關(guān)于windows系統(tǒng)上完全卸載并重裝Node的步驟,需要的朋友可以參考下2023-03-03
利用nodejs讀取圖片并將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成base64格式
這篇文章主要介紹了利用nodejs讀取圖片并將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成base64格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
nodejs開(kāi)發(fā)——express路由與中間件
本篇文章主要介紹了nodejs開(kāi)發(fā)——express路由與中間件 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Linux使用Node.js建立訪問(wèn)靜態(tài)網(wǎng)頁(yè)的服務(wù)實(shí)例詳解
這篇文章主要介紹了Linux使用Node.js建立訪問(wèn)靜態(tài)網(wǎng)頁(yè)的服務(wù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
輕松創(chuàng)建nodejs服務(wù)器(3):代碼模塊化
這篇文章主要介紹了輕松創(chuàng)建nodejs服務(wù)器(3):代碼模塊化,本文是對(duì)第一節(jié)的例子作了封裝,需要的朋友可以參考下2014-12-12
nodejs 使用nodejs-websocket模塊實(shí)現(xiàn)點(diǎn)對(duì)點(diǎn)實(shí)時(shí)通訊
這篇文章主要介紹了nodejs 使用nodejs-websocket模塊實(shí)現(xiàn)點(diǎn)對(duì)點(diǎn)實(shí)時(shí)通訊的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
使用Puppeteer實(shí)現(xiàn)頁(yè)面遍歷的示例代碼
很多時(shí)候我們需要遍歷我們的頁(yè)面來(lái)檢查頁(yè)面是否存在問(wèn)題,以更好的保證可用性和安全性,下面就來(lái)講講如何使用puppeteer來(lái)實(shí)現(xiàn)頁(yè)面遍歷的功能吧2023-06-06

