linux word文件解析,Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容

前言

想要做全文搜索引擎,则需要将word/pdf等文档内容提取出来。对于pdf有xpdf等一些开源方案。

但Word文档的情况则会复杂一些。

提取PDF文本内容

XPDF是一个免费开源的软件,用于显示PDF文件,并可将pdf转换成文字图片等,同样支持Windows版。在Debian Linux上安装非常简单:

我们这里只使用pdftotext这个功能,直接输入可查看帮助:

[]

-f : first page to convert

-l : last page to convert

-r : resolution,in DPI (default is 72)

-x : x-coordinate of the crop area top left corner

-y : y-coordinate of the crop area top left corner

-W : width of crop area in pixels (default is 0)

-H : height of crop area in pixels (default is 0)

-layout : maintain original physical layout

-fixed : assume fixed-pitch (or tabular) text

-raw : keep strings in content stream order

-htmlMeta : generate a simple HTML file,including the Meta information

-enc : output text encoding name

-listenc : list available encodings

-eol : 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 : owner password (for encrypted files)

-upw : 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

测试一下:

然后在node.js中使用 child_process直接调用此命令即可,pdftotext会将内容输出以文本文件中,可能需要多一些操作。具体代码略。

用antiword提取 .doc 的内容

我们这里使用了 antiword 开源软件,来提取word2003以前版本的内容,安装同样非常简单:

查看帮助:

Adobe PDF output

-p PostScript output

paper size like: a4,letter or legal

-x character mapping file

-w in characters of text output

-i image level (PostScript only)

-L use landscape mode (PostScript only)

-r Show removed text

-s Show hidden (by Word) text

antiword直接将word内容输出到了console中:

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的库,

如:

等。

总结

以上就是这文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对编程之家的支持。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值