Java提取Doc,Docx中的文本信息

本文介绍了一种使用Java从Doc和Docx文件中提取文本的方法。通过Apache POI库,可以有效处理这两种格式的文档,适用于文档信息提取的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java提取Doc,Docx中的文本信息

1 代码效果

案例word,Doc,Docx2个版本都有
在这里插入图片描述
提取后效果docx
在这里插入图片描述
提取后效果doc
在这里插入图片描述

2 代码实现

import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * 将doc/docx 部分信息提取
 * 推荐pom jdk1.8+
 * 	<dependency>
	  <groupId>commons-io</groupId>
	  <artifactId>commons-io</artifactId>
	  <version>2.9.0</version>
	</dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.12.0</version>
    </dependency>
 */
public class TestWordToXml {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {
			final List<String> data = new ArrayList<String>();
			final String attr = ".doc";
			final String inPath = "F:\\BiaoQian" + attr;
			final InputStream inputStream = Files.newInputStream(Paths.get(inPath));
			if (inPath.endsWith("docx")) {//docx处理
				final XWPFDocument docx = new XWPFDocument(inputStream).getXWPFDocument();
				final Node bodyNode = docx.getDocument().getBody().getDomNode();
				seeDocxNode(bodyNode,data);
			} else {//doc处理
				final HWPFDocument document = new HWPFDocument(inputStream);
				System.out.println(document.getDocumentText().replaceAll("", " "));
				//data.add(document.getDocumentText().replaceAll("", " "));
			}
			Files.write(Paths.get("F:\\BiaoQianXml.log"),data,Charset.forName("UTF-8"));
		    System.out.println("解析完成");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 递归遍历node
	 * @param bodyNode
	 * @param data
	 */
	@SuppressWarnings("static-access")
	private static void seeDocxNode(final Node node,final List<String> data) {
		final NodeList list = node.getChildNodes();
		final NamedNodeMap attr = node.getAttributes();
		String attrStr = "0";
		String childStr = "";
		String childStrName = "";
		if (attr!=null) {
			attrStr = "" + attr.getLength();
		}
		if (list!=null) {
			childStr = "" + list.getLength();
			for (int i = 0; i < list.getLength(); i++) {
				final Node childNode = list.item(i);
				childStrName = childNode.getNodeName()+","+childStrName;
			}
			if (list.getLength()>0) {
				childStrName = "\t子节点名称["+childStrName.substring(0,childStrName.length()-1)+"]";
			}
		}
		String zhi = "";
		if (StringUtils.isNotEmpty(node.getNodeValue())) {
			zhi = "\t节点值:"+node.getNodeValue();
		}
		if (node.TEXT_NODE==node.getNodeType()) {//放开这个可解析所有结构
			data.add("节点类型:"+node.getNodeType()+"\t属性个数:"+attrStr+"\t子节点个数:"+childStr+"\t节点名:"+node.getNodeName()+childStrName+zhi);
		}
		if (list!=null) {
			for (int i = 0; i < list.getLength(); i++) {
				final Node childNode = list.item(i);
				childStrName = childStrName + "," + childNode.getNodeName();
				seeDocxNode(childNode, data);
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值