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 判断元素上是否绑定了事件
Oct 28 Javascript
JavaScript中的ubound函数使用实例
Nov 04 Javascript
jQuery实现可用于博客的动态滑动菜单
Mar 09 Javascript
JavaScript控制图片加载完成后调用回调函数的方法
Mar 20 Javascript
原生JS:Date对象全面解析
Sep 06 Javascript
javascript实现Java中的Map对象功能的实例详解
Aug 21 Javascript
js封装成插件的步骤方法
Sep 11 Javascript
Angular搜索 过滤 批量删除 添加 表单验证功能集锦(实例代码)
Oct 25 Javascript
详解js正则表达式验证时间格式xxxx-xx-xx形式
Feb 09 Javascript
Vue源码探究之状态初始化
Nov 14 Javascript
Node绑定全局TraceID的实现方法
Nov 14 Javascript
js实现纯前端压缩图片
Nov 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学习笔记 数组的常用函数
2011/06/13 PHP
探讨PHP使用eAccelerator的API开发详解
2013/06/09 PHP
PHP5.3新特性小结
2016/02/14 PHP
Yii+MYSQL锁表防止并发情况下重复数据的方法
2016/07/14 PHP
php正则修正符用法实例详解
2016/12/29 PHP
TP5框架简单登录功能实现方法示例
2019/10/31 PHP
JavaScript 拾漏补遗
2009/12/27 Javascript
JS 按钮点击触发(兼容IE、火狐)
2013/08/07 Javascript
NodeJS中利用Promise来封装异步函数
2015/02/25 NodeJs
JS实现漂亮的淡蓝色滑动门效果代码
2015/09/23 Javascript
JavaScript头像上传插件源码分享
2016/03/29 Javascript
ReactJs快速入门教程(精华版)
2016/11/28 Javascript
vue动态组件实现选项卡切换效果
2017/03/08 Javascript
windows下vue-cli导入bootstrap样式
2017/04/25 Javascript
JS操作时间 - UNIX时间戳的简单介绍(必看篇)
2017/08/16 Javascript
vuejs使用$emit和$on进行组件之间的传值的示例
2017/10/04 Javascript
Nodejs Express 通过log4js写日志到Logstash(ELK)
2018/08/30 NodeJs
JS/jQuery实现简单的开关灯效果【案例】
2019/02/19 jQuery
layUI实现三级导航菜单效果
2019/07/26 Javascript
vue改变循环遍历后的数据实例
2019/11/07 Javascript
JS Ajax请求会话过期处理问题解决方法分析
2019/11/16 Javascript
浅析Vue下的components模板使用及应用
2019/11/27 Javascript
浅谈vue项目利用Hbuilder打包成APP流程,以及遇到的坑
2020/09/12 Javascript
django模型中的字段和model名显示为中文小技巧分享
2014/11/18 Python
python多线程操作实例
2014/11/21 Python
Python中的super用法详解
2015/05/28 Python
深入浅析python中的多进程、多线程、协程
2016/06/22 Python
详解Python3.6的py文件打包生成exe
2018/07/13 Python
使用python采集脚本之家电子书资源并自动下载到本地的实例脚本
2018/10/23 Python
如何使用python操作vmware
2019/07/27 Python
Python 依赖库太多了该如何管理
2019/11/08 Python
Django单元测试中Fixtures的使用方法
2020/02/26 Python
蔬菜基地的创业计划书
2014/01/06 职场文书
导游词之上饶龟峰
2019/10/25 职场文书
MySQL 慢查询日志深入理解
2021/04/22 MySQL
Nginx实现负载均衡的项目实践
2022/03/18 Servers