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中的作用域链和闭包
Jun 30 Javascript
js复制网页内容并兼容各主流浏览器的代码
Dec 17 Javascript
javascript实现禁止右键和F12查看源代码
Dec 26 Javascript
JavaScript数据类型之基本类型和引用类型的值
Apr 01 Javascript
JavaScript中setFullYear()方法的使用详解
Jun 11 Javascript
angular分页指令操作
Jan 09 Javascript
canvas 实现中国象棋
Feb 17 Javascript
JavaScript中undefined和null的区别
May 03 Javascript
angular.js指令中的controller、compile与link函数的不同之处
May 10 Javascript
layui动态渲染生成左侧3级菜单的方法(根据后台返回数据)
Sep 23 Javascript
Vue 图片压缩并上传至服务器功能
Jan 15 Javascript
vuecli项目构建SSR服务端渲染的实现
Oct 30 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生成月历代码
2007/06/14 PHP
如何给phpcms v9增加类似于phpcms 2008中的关键词表
2013/07/01 PHP
php实现基于微信公众平台开发SDK(demo)扩展的方法
2014/12/22 PHP
JavaScript加密解密7种方法总结分析
2007/10/07 Javascript
IE JS编程需注意的内存释放问题
2009/06/23 Javascript
两种WEB下的模态对话框 (asp.net或js的分别实现)
2009/12/02 Javascript
Google (Local) Search API的简单使用介绍
2013/11/28 Javascript
json的定义、标准格式及json字符串检验
2014/05/11 Javascript
jQuery实现360°全景拖动展示
2015/03/18 Javascript
js实现缓冲运动效果的方法
2015/04/10 Javascript
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
JS实现双击编辑可修改状态的方法
2015/08/14 Javascript
easyui导出excel无法弹出下载框的快速解决方法
2016/11/10 Javascript
AngularJS表单基本操作
2017/01/09 Javascript
微信小程序商城项目之商品属性分类(4)
2017/04/17 Javascript
解决微信二次分享不显示摘要和图片的问题
2017/08/18 Javascript
详解weex默认webpack.config.js改造
2018/01/08 Javascript
node.js基础知识小结
2018/02/26 Javascript
详解在vue-cli中使用graphql即vue-apollo的用法
2018/09/08 Javascript
JavaScript队列结构Queue实现过程解析
2020/03/07 Javascript
JS 设计模式之:单例模式定义与实现方法浅析
2020/05/06 Javascript
解决微信授权成功后点击按返回键出现空白页和报错的问题
2020/06/08 Javascript
在vue中axios设置timeout超时的操作
2020/09/04 Javascript
详解Python爬虫的基本写法
2016/01/08 Python
关于Python3 类方法、静态方法新解
2019/08/30 Python
详解python中的lambda与sorted函数
2020/09/04 Python
Python selenium实现断言3种方法解析
2020/09/08 Python
matplotlib 三维图表绘制方法简介
2020/09/20 Python
python BeautifulSoup库的安装与使用
2020/12/17 Python
3D动画《斗罗大陆》上线当日播放过亿
2021/03/16 国漫
Marc O’Polo俄罗斯官方在线商店:德国高端时尚品牌
2019/12/26 全球购物
2015年林业工作总结
2015/05/14 职场文书
未婚证明格式
2015/06/15 职场文书
劳保用品管理制度范本
2015/08/06 职场文书
医院病假条怎么写
2015/08/17 职场文书
解决tk mapper 通用mapper的bug问题
2021/06/16 Java/Android