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实现的分页函数
Feb 07 Javascript
讨论javascript(一)工厂方式 js面象对象的定义方法
Dec 15 Javascript
将string解析为json的几种方式小结
Nov 11 Javascript
表格单元格交错着色实现思路及代码
Apr 01 Javascript
jQuery图片滚动图片的效果(另类实现)
Jun 02 Javascript
JavaScript中Date.toSource()方法的使用教程
Jun 12 Javascript
jQuery Ajax 上传文件处理方式介绍(推荐)
Jun 30 Javascript
微信小程序 下拉菜单简单实例
Apr 13 Javascript
激动人心的 Angular HttpClient的源码解析
Jul 10 Javascript
jQuery实现鼠标点击处心形漂浮的炫酷效果示例
Apr 12 jQuery
Vue条件循环判断+计算属性+绑定样式v-bind的实例
Sep 18 Javascript
JS中通过url动态获取图片大小的方法小结(两种方法)
Oct 31 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
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
2010/03/15 PHP
简单PHP上传图片、删除图片实现代码
2010/05/12 PHP
慎用preg_replace危险的/e修饰符(一句话后门常用)
2013/06/19 PHP
php数组键值用法实例分析
2015/02/27 PHP
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
2015/12/08 PHP
简单谈谈PHP中strlen 函数
2016/02/27 PHP
PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
2016/03/10 PHP
PHP实现的简单对称加密与解密方法实例小结
2017/08/28 PHP
初学JavaScript_03(ExtJs Grid的简单使用)
2008/10/02 Javascript
JavaScript 动态加载脚本和样式的方法
2015/04/13 Javascript
JavaScript中自带的 reduce()方法使用示例详解
2016/08/10 Javascript
JSON 必知必会 观后记
2016/10/27 Javascript
详解jQuery同步Ajax带来的UI线程阻塞问题及解决办法
2017/08/09 jQuery
jQuery EasyUI Layout实现tabs标签的实例
2017/09/26 jQuery
vuex中的 mapState,mapGetters,mapActions,mapMutations 的使用
2018/04/13 Javascript
在微信小程序中渲染HTML内容3种解决方案及分析与问题解决
2020/01/12 Javascript
详解vue实现坐标拾取器功能示例
2020/11/18 Vue.js
Python常见数字运算操作实例小结
2019/03/22 Python
Tornado实现多进程/多线程的HTTP服务详解
2019/07/25 Python
Python Multiprocessing多进程 使用tqdm显示进度条的实现
2019/08/13 Python
详解Anconda环境下载python包的教程(图形界面+命令行+pycharm安装)
2019/11/11 Python
python从zip中删除指定后缀文件(推荐)
2019/12/05 Python
python通过文本在一个图中画多条线的实例
2020/02/21 Python
TensorFlow实现模型断点训练,checkpoint模型载入方式
2020/05/26 Python
Django中的模型类设计及展示示例详解
2020/05/29 Python
python框架flask入门之路由及简单实现方法
2020/06/07 Python
python时间序列数据转为timestamp格式的方法
2020/08/03 Python
Python可以用来做什么
2020/11/23 Python
俄语地区最大的中国商品在线购物网站之一:Umka Mall
2019/11/03 全球购物
什么是GWT的Module
2013/01/20 面试题
开展党的群众路线教育实践活动方案
2014/02/05 职场文书
廉洁校园实施方案
2014/05/25 职场文书
先进个人总结范文
2015/02/15 职场文书
机械原理课程设计心得体会
2016/01/15 职场文书
关于拾金不昧的感谢信(五篇)
2019/10/18 职场文书
Python开发之QT解决无边框界面拖动卡屏问题(附带源码)
2021/05/27 Python