当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容

Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容

2017年12月12日  | 移动技术网IT编程  | 我要评论

前言

想要做全文搜索引擎,则需要将word/pdf等文档内容提取出来。对于pdf有xpdf等一些开源方案。

但word文档的情况则会复杂一些。

提取pdf文本内容

xpdf是一个免费开源的软件,用于显示pdf文件,并可将pdf转换成文字图片等,同样支持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

测试一下:

root@raspberrypi:/var/www# pdftotext onceai.pdf onceai.txt
root@raspberrypi:/var/www# cat onceai.txt 产品介绍 顽石智能科技(上海)有限公司
....

然后在node.js中使用 child_process直接调用此命令即可,pdftotext会将内容输出以文本文件中,可能需要多一些操作。具体代码略。

用antiword提取 .doc 的内容

我们这里使用了 antiword 开源软件,来提取word2003以前版本的内容,安装同样非常简单:

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内容输出到了console中:

root@raspberrypi:/var/www# antiword spec.doc

sync mobile – ford apa
project number: dfyst
requirements specification

同样在node.js用child_process调用此命令即可。

解析提取.docx 的内容

对于 docx 文档来说,因基本身就是一个zip文件,只需要在node.js先将其解压,再解析 text.docx\word\document.xml 文件即可。

github上也有些将docx解析成html的库,

如:

 

 

等。

总结

以上就是这文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网