Apache POI - HWPF and XWPF - Java API to Handle Microsoft Word Files

http://poi.apache.org/document/

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.poi/poi-scratchpad/3.7

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.poi/poi-ooxml/3.7

对Doc文件的解析

需要poi-scratchpad/3.7.jar

POI-HWPF - A Quick Guide

基本的文本提取

有两个输入参数:inputstream,HWPFDocument,

getText()方法是得到所有的文本内容,

getParagraphText()是得到每一段的文本内容,

getTextFromPieces()是得到每一页的文本内容

特定文本属性提取

To get specific bits of text, first create a org.apache.poi.hwpf.HWPFDocument. Fetch the range with getRange(), then get paragraphs from that. You can then get text and other properties.

第一步:创建HWPFDocument

第二步:得到Range

getRange(): Returns the range which covers the whole of the document, but excludes any headers(页眉) and footers(页脚).

 int numParagraphs()            Used to get the number of paragraphs in a range.
 int numSections()            Used to get the number of sections in a range(这个是“节”,就是插入、分隔符中的“节”)

第三步:得到段落

getParagraph():

getText()

public static void main(String[] args) throws Exception {
InputStream istream = new FileInputStream(
"e:\\Users\\ywf\\Desktop\\文本校对\\1.docx");
HWPFDocument doc = new HWPFDocument(istream);
Range range = doc.getRange();// Returns the range which covers the whole
// of the document, but excludes any
// headers and footers.
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph poiPara = range.getParagraph(i);
int j = 0;
while (true) {
CharacterRun run = poiPara.getCharacterRun(j++);
System.out.println("Color " + run.getColor());//颜色
System.out.println("Font size " + run.getFontSize());//字体大小
System.out.println("Font Name " + run.getFontName());//字体名称
System.out.println(run.isBold() + " " + run.isItalic() + " "
+ run.getUnderlineCode());//加粗,斜体,下划线
System.out.println("Text is " + run.text());//文本内容
if (run.getEndOffset() == poiPara.getEndOffset()) {
break;
}
}
} }

对Docx文件的解析

需要poi-ooxml/3.7.jar

http://poi.apache.org/document/quick-guide-xwpf.html

package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun; public class ParseWordDocxTest { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
InputStream istream = new FileInputStream(
"e:\\Users\\ywf\\Desktop\\文本校对\\1.docx");
XWPFDocument docx = new XWPFDocument(istream);
List<XWPFParagraph> paraGraph = docx.getParagraphs();
for(XWPFParagraph para :paraGraph ){
List<XWPFRun> run = para.getRuns();
for(XWPFRun r : run){
int i = 0;
System.out.println("字体颜色:"+r.getColor());
System.out.println("字体名称:"+r.getFontFamily());
System.out.println("字体大小:"+r.getFontSize());
System.out.println("Text:"+r.getText(i++));
System.out.println("粗体?:"+r.isBold());
System.out.println("斜体?:"+r.isItalic()); }
} } }

Tika解析word文件的更多相关文章

  1. C#仪器数据文件解析-Word文件(doc、docx)

    不少仪器数据报告输出为Word格式文件,同Excel文件,Word文件doc和docx的存储格式是不同的,相应的解析Word文件的方式也类似,主要有以下方式: 1.通过MS Word应用程序的DCOM ...

  2. 用python解析word文件(二):table

    太长了,我决定还是拆开三篇写.   (一)段落篇(paragraph) (二)表格篇(table)(本篇) (三)样式篇(style) 选你所需即可.下面开始正文. 上一篇我们讲了用python-do ...

  3. 用python解析word文件(一):paragraph

    太长了,我决定还是拆开三篇写.   (一)段落篇(paragraph)(本篇) (二)表格篇(table) (三)样式篇(style) 选你所需即可.下面开始正文. 最近公司的项目,需要在页面上显示w ...

  4. 用python解析word文件(三):style

    太长了,我决定还是拆开三篇写.   (一)段落篇(paragraph) (二)表格篇(table) (三)样式篇(style)(本篇) 选你所需即可.下面开始正文. 在前两篇中,我们已经解析出了par ...

  5. 用python解析word文件(段落篇(paragraph) 表格篇(table) 样式篇(style))

    首先需要安装相应的支持库: 直接在命令行执行pip install python-docx 示例代码如下: import docxfrom docx import Document #导入库 path ...

  6. 用python读取word文件里的表格信息【华为云技术分享】

    在企查查查询企业信息的时候,得到了一些word文件,里面有些控股企业的数据放在表格里,需要我们将其提取出来. word文件看起来很复杂,不方便进行结构化.实际上,一个word文档中大概有这么几种类型的 ...

  7. NodeJs之word文件生成与解析

    NodeJs之word文件生成与解析 一,介绍与需求 1.1,介绍 1,officegen模块可以为Microsoft Office 2007及更高版本生成Office Open XML文件.此模块不 ...

  8. Apache-Tika解析Word文档

    通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理Word格式的文章,如下: package com.mengyao.tika.app; i ...

  9. Java读取word文件,字体,颜色

    在Android读取Word文件时,在网上查看时可以用tm-extractors,但好像没有提到怎么读取Word文档中字体的颜色,字体,上下标等相关的属性.但由于需要,要把doc文档中的内容(字体,下 ...

随机推荐

  1. 洛谷 P3391 模板Splay

    #include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...

  2. faster-rcnn 目标检测 数据集制作

    本文的目标是制作目标检测的数据集 使用的工具是 python + opencv 实现目标 1.批量图片重命名,手动框选图片中的目标,将目标框按照一定格式保存到txt中 图片名格式(批量) .jpg . ...

  3. hdu 3613 KMP算法扩展

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. Java-堆排序

    public class Main { public static void main(String[] args) { int a[] = {8, 2, 5, 6, 4, 8, 9, 7, 14, ...

  5. 【CF56E】Domino Principle(线性扫描,伪DP)

    每块多米诺骨牌所在的位置设为x,每块多米诺骨牌高度为h.如果将x位置上的多米诺骨牌向右翻到,它就可以影响[x+1, x+h-1]范围内的所有多米诺骨牌,让他们也翻到,同时这些被翻到的多米诺骨牌还能影响 ...

  6. 打印倒序NxN乘法表

    一.实验要求: 给定任意一个字符N(N>0),然后打印NxN的倒序乘法表. 二.解决问题: #/!bin/bash# #define functionNxN_fun(){ local i=$1 ...

  7. [转]结队开发之多storyboard

    转自Haven's Blog   Storyboard的出现,让开发变得像讲故事一样,UI间的关系流程也一目了然.它其实是xib的升级版本,将多个xib统一管理了.任何事都有双面性,Storyboar ...

  8. npm 查看模块全部版本

    npm 查看模块全部版本:(jquery) npm view jquery versions 安装指定版本: (jquery) npm install jquery@1.7.2

  9. TF-IDF 实践

    打算分以下几个部分进行 1. 用python写一个爬虫爬取网易新闻 2. 用分词工具对爬下来的文字进行处理, 形成语料库 3. 根据TF-IDF, 自动找出新闻的关键词 4. 根据TF-IDF, 实现 ...

  10. 作为程序员,再也不想和PM干架了

    上周,又看见有程序和PM(产品经理)吵了起来,大致是因为晚上就要上线了,下午的时候PM来说要改点需求,但程序不愿意.兴许是天气热了,大家都很烦躁,于是一言不合就发飙了,最终还是程序老大介入才解决了问题 ...