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 相关文章推荐
js中判断Object、Array、Function等引用类型对象是否相等
Aug 29 Javascript
javascript实现阻止iOS APP中的链接打开Safari浏览器
Jun 12 Javascript
JavaScript中string转换成number介绍
Dec 31 Javascript
AngularJS基础学习笔记之指令
May 10 Javascript
jQuery实现点击水纹波动动画
Apr 10 Javascript
vuejs如何配置less
Apr 25 Javascript
Ajax高级笔记 JavaScript高级程序设计笔记
Jun 22 Javascript
css和js实现弹出登录居中界面完整代码
Nov 26 Javascript
解决angular2在双向数据绑定时[(ngModel)]无法使用的问题
Sep 13 Javascript
jQuery中DOM常见操作实例小结
Aug 01 jQuery
浅析webpack-bundle-analyzer在vue-cli3中的使用
Oct 23 Javascript
three.js着色器材质的内置变量示例详解
Aug 16 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
解决PHP超大文件下载,断点续传下载的方法详解
2013/06/06 PHP
php显示指定目录下子目录的方法
2015/03/20 PHP
PHP三种方式实现链式操作详解
2017/01/21 PHP
实现laravel 插入操作日志到数据库的方法
2019/10/11 PHP
DOM精简教程
2006/10/03 Javascript
深入理解JavaScript系列(4) 立即调用的函数表达式
2012/01/15 Javascript
jQuery弹出层始终垂直居中相对于屏幕或当前窗口
2013/04/01 Javascript
JS 实现列表与多选框选择附预览动画
2014/10/29 Javascript
使用AngularJS实现表单向导的方法
2015/06/19 Javascript
JS获取IE版本号与HTML设置IE文档模式的方法
2016/10/09 Javascript
完美解决jQuery 鼠标快速滑过后,会执行多次滑出的问题
2016/12/08 Javascript
Bootstrap表单简单实现代码
2017/03/06 Javascript
jQuery Json数据格式排版高亮插件json-viewer.js使用方法详解
2017/06/12 jQuery
详解plotly.js 绘图库入门使用教程
2018/02/23 Javascript
.vue文件 加scoped 样式不起作用的解决方法
2018/05/28 Javascript
解决vue项目打包后提示图片文件路径错误的问题
2018/07/04 Javascript
详解Koa中更方便简单发送响应的方式
2018/07/20 Javascript
opencv 识别微信登录验证滑动块位置
2018/08/07 Javascript
微信小程序实现图片选择并预览功能
2019/07/25 Javascript
Vue常用传值方式、父传子、子传父及非父子实例分析
2020/02/24 Javascript
[01:18]一目了然!DOTA2DotA快捷操作对比第一弹
2014/07/01 DOTA
[04:31]2016国际邀请赛中国区预选赛妖精采访
2016/06/27 DOTA
[01:06]欢迎来到上海,TI9
2018/08/26 DOTA
Python基于socket实现简单的即时通讯功能示例
2018/01/16 Python
Python查找最长不包含重复字符的子字符串算法示例
2019/02/13 Python
PyQt5实现QLineEdit添加clicked信号的方法
2019/06/25 Python
8段用于数据清洗Python代码(小结)
2019/10/31 Python
利用python绘制正态分布曲线
2021/01/04 Python
python利用文件时间批量重命名照片和视频
2021/02/09 Python
Lentiamo丹麦:购买便宜的隐形眼镜
2021/01/13 全球购物
学生实习自我鉴定
2013/10/11 职场文书
市场营销专科应届生求职信
2013/11/24 职场文书
派出所班子党的群众路线对照检查材料思想汇报
2014/10/01 职场文书
平安家庭事迹材料
2014/12/20 职场文书
优秀大学生事迹材料
2014/12/24 职场文书
vue项目proxyTable配置和部署服务器
2022/04/14 Vue.js