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

 更新时间:2017年06月17日 14:53:36   作者:kris  
这篇文章主要给大家介绍了关于Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面跟着小编来一起看看吧。

前言

想要做全文搜索引擎,则需要将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的库,

如:

https://github.com/mwilliamson/mammoth.js 

https://github.com/lalalic/docx2html 

等。

总结

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

相关文章

  • windows系统上完全卸载并重装Node的步骤(亲测可用)

    windows系统上完全卸载并重装Node的步骤(亲测可用)

    对于Windows平台来说,所有的应用程序,其安装卸载都是一样的,node.js也不例外,但是还是很多用户不明白,下面这篇文章主要给大家介绍了关于windows系统上完全卸载并重装Node的步骤,需要的朋友可以参考下
    2023-03-03
  • 利用nodejs读取图片并将二进制数据转换成base64格式

    利用nodejs读取图片并将二进制数据转换成base64格式

    这篇文章主要介绍了利用nodejs读取图片并将二进制数据转换成base64格式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • nodejs开发——express路由与中间件

    nodejs开发——express路由与中间件

    本篇文章主要介绍了nodejs开发——express路由与中间件 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • NodeJS远程代码执行

    NodeJS远程代码执行

    这篇文章主要介绍了NodeJS远程代码执行方法的相关资料,需要的朋友可以参考下
    2016-08-08
  • Node.js中你不可不精的Stream(流)

    Node.js中你不可不精的Stream(流)

    这篇文章主要给大家介绍了关于Node.js中你不可不精的Stream(流)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-06-06
  • 全面了解Node事件循环

    全面了解Node事件循环

    这篇文章主要详细介绍了Node事件循环,文中的图片以及文字讲解可以帮助大家更好的了解事件循环,感兴趣的小伙伴可以看一看,多多参考一下
    2021-08-08
  • Linux使用Node.js建立访问静态网页的服务实例详解

    Linux使用Node.js建立访问静态网页的服务实例详解

    这篇文章主要介绍了Linux使用Node.js建立访问静态网页的服务实例详解的相关资料,需要的朋友可以参考下
    2017-03-03
  • 轻松创建nodejs服务器(3):代码模块化

    轻松创建nodejs服务器(3):代码模块化

    这篇文章主要介绍了轻松创建nodejs服务器(3):代码模块化,本文是对第一节的例子作了封装,需要的朋友可以参考下
    2014-12-12
  • nodejs 使用nodejs-websocket模块实现点对点实时通讯

    nodejs 使用nodejs-websocket模块实现点对点实时通讯

    这篇文章主要介绍了nodejs 使用nodejs-websocket模块实现点对点实时通讯的实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • 使用Puppeteer实现页面遍历的示例代码

    使用Puppeteer实现页面遍历的示例代码

    很多时候我们需要遍历我们的页面来检查页面是否存在问题,以更好的保证可用性和安全性,下面就来讲讲如何使用puppeteer来实现页面遍历的功能吧
    2023-06-06

最新评论