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


Posted in Javascript onJune 17, 2017

前言

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

等。

总结

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

Javascript 相关文章推荐
javascript 关闭IE6、IE7
Jun 01 Javascript
jquery实现的让超出显示范围外的导航自动固定屏幕最顶上
Sep 22 Javascript
JavaScript中的null和undefined解析
Apr 14 Javascript
Javascript 面向对象(二)封装代码
May 23 Javascript
jQuery链式调用与show知识浅析
May 11 Javascript
基于Javascript实现的不重复ID的生成器
Dec 25 Javascript
Angularjs2不同组件间的通信实例代码
May 06 Javascript
Javascript中Promise的四种常用方法总结
Jul 14 Javascript
前端主流框架vue学习笔记第二篇
Jul 26 Javascript
JS实现留言板功能[楼层效果展示]
Dec 27 Javascript
vue3弹出层V3Popup实例详解
Jan 04 Vue.js
jquery插件实现搜索历史
Apr 24 jQuery
bootstrap paginator分页前后台用法示例
Jun 17 #Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
Jun 17 #Javascript
深入理解Webpack 中路径的配置
Jun 17 #Javascript
详解Vue组件之间的数据通信实例
Jun 17 #Javascript
webpack打包后直接访问页面图片路径错误的解决方法
Jun 17 #Javascript
webpack学习教程之publicPath路径问题详解
Jun 17 #Javascript
详解windows下vue-cli及webpack 构建网站(三)使用组件
Jun 17 #Javascript
You might like
一个简易需要注册的留言版程序
2006/10/09 PHP
小谈php正则提取图片地址
2014/03/27 PHP
解决php的“It is not safe to rely on the system’s timezone settings”问题
2015/10/08 PHP
php对二维数组进行相关操作(排序、转换、去空白等)
2015/11/04 PHP
PHP可变变量学习小结
2015/11/29 PHP
深入理解PHP JSON数组与对象
2016/07/19 PHP
JavaScript中为元素加上name属性的方法
2011/05/09 Javascript
输入密码检测大写是否锁定js实现代码
2012/12/03 Javascript
引用外部脚本时script标签关闭的写法
2014/01/20 Javascript
ie下$.getJSON出现问题的解决方法
2014/02/12 Javascript
javascript实现链接单选效果的方法
2015/05/13 Javascript
jquery 重写 ajax提交并判断权限后 使用load方法报错解决方法
2016/01/19 Javascript
微信小程序 toast 详解及实例代码
2016/11/09 Javascript
AngularJS基于ngInfiniteScroll实现下拉滚动加载的方法
2016/12/14 Javascript
JavaScript之promise_动力节点Java学院整理
2017/07/03 Javascript
浅谈react+es6+webpack的基础配置
2017/08/09 Javascript
json解析大全 双引号、键值对不在一起的情况
2019/12/06 Javascript
Express 配置HTML页面访问的实现
2020/11/01 Javascript
浅析Python中将单词首字母大写的capitalize()方法
2015/05/18 Python
将python代码和注释分离的方法
2018/04/21 Python
Python 利用切片从列表中取出一部分使用的方法
2019/02/01 Python
对Python之gzip文件读写的方法详解
2019/02/08 Python
处理Selenium3+python3定位鼠标悬停才显示的元素
2019/07/31 Python
python中编写函数并调用的知识点总结
2021/01/13 Python
纯css3实现的竖形无限级导航
2014/12/10 HTML / CSS
台湾SHOPRO购物行家:亚洲首创影视.3C.家电.优质购物平台
2018/05/07 全球购物
美国转售二手商品的电子商务平台:BLINQ
2018/12/13 全球购物
体育学院毕业生自荐信
2013/11/03 职场文书
优秀教师先进事迹
2014/01/22 职场文书
创业计划实施的7大步骤
2014/02/05 职场文书
大学专科自荐信
2014/06/17 职场文书
员工工作能力评语
2014/12/31 职场文书
机器人总动员观后感
2015/06/09 职场文书
工作一年自我鉴定
2019/06/20 职场文书
《亲亲我的妈妈》观后感(3篇)
2019/09/26 职场文书
Redis高并发防止秒杀超卖实战源码解决方案
2021/11/01 Redis