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 相关文章推荐
jquery插件validate验证的小例子
May 08 Javascript
jquery ajax中使用jsonp的限制解决方法
Nov 22 Javascript
jQuery事件之键盘事件(ctrl+Enter回车键提交表单等)
May 11 Javascript
jQuery实现个性翻牌效果导航菜单的方法
Mar 09 Javascript
js时间戳格式化成日期格式的多种方法介绍
Feb 16 Javascript
JavaScript仿微信打飞机游戏
Jul 05 Javascript
基于angular实现三级联动的生日插件
May 12 Javascript
javaScript日期工具类DateUtils详解
Dec 08 Javascript
webpack+react+antd脚手架优化的方法
Apr 02 Javascript
vue-cli3中配置alias和打包加hash值操作
Sep 04 Javascript
微信小程序调用后台service教程详解
Nov 06 Javascript
解决基于 keep-alive 的后台多级路由缓存问题
Dec 23 Javascript
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
VFP与其他应用程序的集成
2006/10/09 PHP
PHP 采集程序 常用函数
2008/12/18 PHP
jQuery Mobile + PHP实现文件上传
2014/12/12 PHP
Yii框架中sphinx索引配置方法解析
2016/10/18 PHP
php从身份证获取性别和出生年月
2017/02/09 PHP
利用PHP_XLSXWriter代替PHPExcel的方法示例
2017/07/16 PHP
javascript 按回车键相应按钮提交事件
2009/11/02 Javascript
Javascript的构造函数和constructor属性
2010/01/09 Javascript
js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版
2011/01/08 Javascript
IE的有条件注释判定IE版本详解(附实例代码)
2012/01/04 Javascript
JavaScript中实现PHP的打乱数组函数shuffle实例
2014/10/11 Javascript
浅谈javascript中自定义模版
2015/01/29 Javascript
JS实现转动随机数抽奖特效代码
2020/04/16 Javascript
jquery仿京东商品放大浏览页面
2017/06/06 jQuery
Vue 前端实现登陆拦截及axios 拦截器的使用
2019/07/17 Javascript
教你如何用Node实现API的转发(某音乐)
2019/09/20 Javascript
js+canvas实现简单扫雷小游戏
2021/01/22 Javascript
JS数组Reduce方法功能与用法实例详解
2020/04/29 Javascript
基于vue.js仿淘宝收货地址并设置默认地址的案例分析
2020/08/20 Javascript
jQuery使用hide()、toggle()函数实现相机品牌展示隐藏功能
2021/01/29 jQuery
简单介绍Python中的filter和lambda函数的使用
2015/04/07 Python
python简单商城购物车实例代码
2018/03/15 Python
Python实现输出某区间范围内全部素数的方法
2018/05/02 Python
解决Pycharm出现的部分快捷键无效问题
2018/10/22 Python
详解python3 + Scrapy爬虫学习之创建项目
2019/04/12 Python
Python实现把类当做字典来访问
2019/12/16 Python
Python @property原理解析和用法实例
2020/02/11 Python
Python xlwt模块使用代码实例
2020/06/10 Python
Python通过fnmatch模块实现文件名匹配
2020/09/30 Python
办公室员工岗位工作职责
2014/03/10 职场文书
小班上学期评语
2014/05/05 职场文书
党课培训心得体会
2014/09/02 职场文书
四风自我剖析材料
2014/09/30 职场文书
个人作风建设剖析材料
2014/10/11 职场文书
python基于tkinter制作m3u8视频下载工具
2021/04/24 Python
ubuntu如何搭建vsftpd服务器
2022/12/24 Servers