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 HTML5 音乐天气播放器(Ajax获取天气信息)
May 26 Javascript
原生Js实现元素渐隐/渐现(原理为修改元素的css透明度)
Jun 24 Javascript
JavaScript实现非常简单实用的下拉菜单效果
Aug 27 Javascript
Angular2 http jsonp的实例详解
Aug 31 Javascript
解决element-ui中下拉菜单子选项click事件不触发的问题
Aug 22 Javascript
vue组件(全局,局部,动态加载组件)
Sep 02 Javascript
vue设置导航栏、侧边栏为公共页面的例子
Nov 01 Javascript
vue之debounce属性被移除及处理详解
Nov 13 Javascript
jQuery弹框插件使用方法详解
May 26 jQuery
Javascript call及apply应用场景及实例
Aug 26 Javascript
vue swipeCell滑动单元格(仿微信)的实现示例
Sep 14 Javascript
在react-antd中弹出层form内容传递给父组件的操作
Oct 24 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 array_push()数组函数:将一个或多个单元压入数组的末尾(入栈)
2011/07/12 PHP
对象失去焦点时自己动提交数据的实现代码
2012/11/06 PHP
phpmyadmin在宝塔面板里进不去的解决方案
2020/07/06 PHP
创建一个复制UBB软件信息的链接或按钮的js代码
2008/01/06 Javascript
jquery mobile changepage的三种传参方法介绍
2013/09/13 Javascript
将两个div左右并列显示并实现点击标题切换内容
2013/10/22 Javascript
jquery实现两个图片渐变切换效果的方法
2015/06/25 Javascript
jQuery获取DOM节点实例分析(2种方式)
2015/12/15 Javascript
jQuery取得iframe中元素的常用方法详解
2016/01/14 Javascript
js仿QQ中对联系人向左滑动、滑出删除按钮的操作
2016/04/07 Javascript
AngularJS入门教程之XHR和依赖注入详解
2016/08/18 Javascript
node.js中的事件处理机制详解
2016/11/26 Javascript
AngularJS的脏检查深入分析
2017/04/22 Javascript
详解Vue 动态添加模板的几种方法
2017/04/25 Javascript
JavaScript字符串转数字的5种方法及遇到的坑
2018/07/16 Javascript
vue搜索页开发实例代码详解(热门搜索,历史搜索,淘宝接口演示)
2020/04/11 Javascript
JQuery省市联动效果实现过程详解
2020/05/08 jQuery
怎样使用Python脚本日志功能
2016/08/14 Python
OpenCV-Python实现轮廓检测实例分析
2018/01/05 Python
Python设计模式之命令模式简单示例
2018/01/10 Python
python 随机打乱 图片和对应的标签方法
2018/12/14 Python
OpenCV哈里斯(Harris)角点检测的实现
2020/01/15 Python
在spyder IPython console中,运行代码加入参数的实例
2020/04/20 Python
Python正则re模块使用步骤及原理解析
2020/08/18 Python
python从PDF中提取数据的示例
2020/10/30 Python
德国运动营养和健身网上商店:Myprotein.de
2018/07/18 全球购物
乌克兰在线商店的价格比较:Price.ua
2019/07/26 全球购物
公司财务流程之主管工作流程
2014/03/03 职场文书
优秀食品类广告词
2014/03/19 职场文书
学校校庆演讲稿
2014/05/22 职场文书
大学生学雷锋活动总结
2014/06/26 职场文书
公安领导班子四风问题个人整改措施思想汇报
2014/10/09 职场文书
四风问题自查自纠工作情况报告
2014/10/28 职场文书
商场圣诞节活动总结
2015/05/06 职场文书
房地产置业顾问工作总结
2015/10/23 职场文书
2016企业先进集体事迹材料
2016/02/25 职场文书