1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java poi之word文本图片内容提取

Java poi之word文本图片内容提取

时间:2021-11-04 19:43:12

相关推荐

Java poi之word文本图片内容提取

目录结构

前言文档准备引入Maven依赖代码块提取结果验证孤勇者提取结果青鸟提取结果对比

前言

应公司需求,需实现以下功能

word文本内容的替换;word文本内容的提取;word文档中图片的提取存放

此文章将使用Apache POI实现Word文档中文本内容及图片的提取;

Apache POI 是基于 Office Open XML 标准(OOXML)和 Microsoft 的 OLE 2 复合文档格式(OLE2)处理各种文件格式的开源项目。 简而言之,您可以使用 Java 读写 MS Excel 文件,可以使用 Java 读写 MS Word 和 MS PowerPoint 文件。

HSSF - 提供读写 Microsoft Excel XLS 格式 (Microsoft Excel 97 (-)) 档案的功能。XSSF - 提供读写 Microsoft Excel OOXML XLSX 格式 (Microsoft Excel XML (+)) 档案的功能。SXSSF - 提供低内存占用量读写 Microsoft Excel OOXML XLSX 格式档案的功能。HWPF - 提供读写 Microsoft Word DOC97 格式 (Microsoft Word 97 (-)) 档案的功能。XWPF - 提供读写 Microsoft Word DOC 格式 (WordprocessingML (+)) 档案的功能。HSLF/XSLF - 提供读写 Microsoft PowerPoint 格式档案的功能。HDGF/XDGF - 提供读 Microsoft Visio 格式档案的功能。HPBF - 提供读 Microsoft Publisher 格式档案的功能。HSMF - 提供读 Microsoft Outlook 格式档案的功能。

文档准备

小编准备了以下两个文档:《孤勇者.doc》《青鸟.docx》,分别代表不同版本的文档,里边分别记录了各自的歌词及宣传画,以便提取测试,如下图

引入Maven依赖

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.2</version></dependency>

代码块

package com.bjzaxk.utils;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.hwpf.extractor.WordExtractor;import org.apache.poi.hwpf.model.PicturesTable;import org.apache.poi.hwpf.usermodel.CharacterRun;import org.apache.poi.hwpf.usermodel.Picture;import org.apache.poi.hwpf.usermodel.Range;import org.apache.poi.xwpf.extractor.XWPFWordExtractor;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFPictureData;import java.io.*;import java.util.List;public class Demo {public static void main(String[] args) {String filePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\孤勇者.doc";String formart = "DOC";// String filePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\青鸟.docx";// String formart = "DOCX";// 文本提取后存放路径及文件名String extractFilePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\孤勇者.txt";// String extractFilePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\青鸟.docx";// 图片提取后存放路径String imageFilePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\";// 提取word中的文字wordTextExtract(filePath, formart, extractFilePath);// 提取word中的图片imageExtract(filePath, formart, imageFilePath);}/*** @description: 提取word中的文字* @author: Mr.Jkx* @time: /1/10 14:17*/public static void wordTextExtract(String filePath, String formart, String extractFilePath) {try {// 创建存放提取数据的文件FileWriter fw = new FileWriter(extractFilePath);BufferedWriter bw = new BufferedWriter(fw);if ("DOCX".equals(formart)) {// 读取docx文件InputStream in = new FileInputStream(filePath);XWPFDocument document = new XWPFDocument(in);XWPFWordExtractor re = new XWPFWordExtractor(document);re.close();// 将内容写入新文件中bw.write(re.getText());bw.close();} else if ("DOC".equals(formart)) {InputStream in = new FileInputStream(filePath);WordExtractor wordExtractor = new WordExtractor(in);// 将内容写入新文件中bw.write(wordExtractor.getText());bw.close();}} catch (Exception e) {e.printStackTrace();}}/*** @description: 提取word中的图片* @author: Mr.Jkx* @time: /1/10 14:26*/public static void imageExtract(String filePath, String formart, String imageFilePath) {FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream(new File(filePath));if ("DOCX".equals(formart)) {XWPFDocument document = new XWPFDocument(fileInputStream);List<XWPFPictureData> picList = document.getAllPictures();for (XWPFPictureData pic : picList) {byte[] bytev = pic.getData();FileOutputStream fos = new FileOutputStream(imageFilePath + pic.getFileName());fos.write(bytev);}fileInputStream.close();} else if ("DOC".equals(formart)) {HWPFDocument doc = new HWPFDocument(fileInputStream);int length = doc.characterLength();PicturesTable pTable = doc.getPicturesTable();for (int i = 0; i < length; i++) {Range range = new Range(i, i + 1, doc);CharacterRun cr = range.getCharacterRun(0);if (pTable.hasPicture(cr)) {Picture pic = pTable.extractPicture(cr, false);String afileName = pic.suggestFullFileName();OutputStream out = new FileOutputStream(new File(imageFilePath + System.currentTimeMillis() + afileName));pic.writeImageContent(out);}}}} catch (IOException e) {e.printStackTrace();} finally {if (fileInputStream != null) {try {// 关闭资源fileInputStream.close();} catch (IOException e) {e.printStackTrace();LoggerSetting.Instance().setWorkInfo("读取Word文件失败...");}}}}}

提取结果验证

提取文本会存放于一个txt文件中(小编工作需求,其他类型文件未尝试!);提取的图片会存放于“imageFilePath ”路径中,准备的文档中只有一张图片,多张图片的情况下,提取完成会在存放路径中有多张图片(在此不再展示);提取过程中不会改变原本word内容;

孤勇者提取结果

青鸟提取结果对比

DOCX类型的文档使用XWPF提取文字,存储后用电脑自带的“文本文档”打开是没有格式的,小编这里用的是“Notepad++”打开截图的,如默认软件打开无格式无须担心,换个软件即可;

XWPF - 提供读写 Microsoft Word DOC 格式 (WordprocessingML (+)) 档案的功能。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。