利用java从docx文档中提取文本内容

使用Apache的第三方jar包,地址为https://poi.apache.org/

docx文档内容如图:



目录结构:



每个文件夹的名称为日期加上来源,例如:20180618医院,每个docx文档的名称是被试的姓名和来源地,例如:小明-xx社区

代码如下:

MriReportService.java

package services;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.regex.Pattern; public class MriReportService { public static String[] findYearAndSource(File file) {
String[] result = new String[2];
// 日期
String dateStr = file.getParentFile().getName();
// System.out.println(dateStr);
if (Pattern.compile("\\d").matcher(dateStr).find()) {
dateStr = Pattern.compile("-").matcher(dateStr).replaceAll("");
result[0] = dateStr.substring(0, 8);
} else {
result[0] = "";
} // 社区
String fileName = file.getName();
if (fileName.indexOf("-") < 0) {
fileName = Pattern.compile("\\.").matcher(fileName).replaceAll("-.");
}
fileName = Pattern.compile("--+").matcher(fileName).replaceAll("-");
result[1] = fileName.substring(fileName.indexOf("-") + 1, fileName.indexOf(".")); return result;
} public static LinkedList<File> findAllFile(String rootPath) {
File file = new File(rootPath);
LinkedList<File> list = new LinkedList<>();
if (file.exists()) {
File[] subDirs = file.listFiles();
for (File tmpDir : subDirs) {
// System.out.println(tmpDir);
for (File tmpFile : tmpDir.listFiles()) {
if (tmpFile.isFile() && tmpFile.getName().indexOf("~$") < 0) {
list.add(tmpFile);
}
}
}
} return list;
} public static ArrayList<String> findSub(String docx) {
String name = "";
String gender = "";
String age = "";
String MRICheck = "";
String MRIMem = ""; if (!Pattern.compile("性别:").matcher(docx).find() || !Pattern.compile("年龄:").matcher(docx).find()) {
try {
name = docx.substring(docx.indexOf("姓名:") + 3, docx.indexOf("检查项目:"));
MRICheck = docx.substring(docx.indexOf("MRI检查描述:") + 8, docx.indexOf("MRI印象:"));
MRIMem = docx.substring(docx.indexOf("MRI印象:") + 6, docx.indexOf("报告医师:"));
} catch (StringIndexOutOfBoundsException e) {
// name = "";
}
} else {
name = docx.substring(docx.indexOf("姓名:") + 3, docx.indexOf("性别:"));
gender = docx.substring(docx.indexOf("性别:") + 3, docx.indexOf("年龄:"));
age = docx.substring(docx.indexOf("年龄:") + 3, docx.indexOf("检查项目:"));
MRICheck = docx.substring(docx.indexOf("MRI检查描述:") + 8, docx.indexOf("MRI印象:"));
MRIMem = docx.substring(docx.indexOf("MRI印象:") + 6, docx.indexOf("报告医师:"));
} ArrayList<String> result = new ArrayList<>();
result.add(name);
result.add(gender);
result.add(age);
result.add(MRICheck);
result.add(MRIMem);
return result;
}
}

Main.java

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.*;
import java.util.ArrayList;
import java.util.regex.*; import static services.MriReportService.findAllFile;
import static services.MriReportService.findSub;
import static services.MriReportService.findYearAndSource; public class Main { public static void main(String[] args) throws Exception { if (args.length < 2) {
System.out.println("请输入源文件和目标文件的完整路径!");
System.out.println("举个例子:java -jar docx2csv.jar d:\\核磁报告 d:\\result.csv");
System.exit(-1);
} String srcPath = args[0];
String outPath = args[1];
ArrayList<ArrayList<String>> result = new ArrayList<>();
for (File tmpFile : findAllFile(srcPath)) { String[] yearAndSrc = findYearAndSource(tmpFile); FileInputStream fis = new FileInputStream(tmpFile);
XWPFDocument xdoc = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
String docx = extractor.getText(); docx = Pattern.compile("\\s").matcher(docx).replaceAll("");
ArrayList<String> tmpRe = findSub(docx);
tmpRe.add(yearAndSrc[0]);
tmpRe.add(yearAndSrc[1]);
result.add(tmpRe);
fis.close();
}
write(result, outPath);
} public static void write(ArrayList<ArrayList<String>> result, String outPath) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outPath), "GBK"));
for (ArrayList<String> tmpStrs : result) {
// System.out.println();
bufferedWriter.write(tmpStrs.get(0) + "," + tmpStrs.get(1) + ","
+ tmpStrs.get(2) + "," + tmpStrs.get(3) + ","
+ tmpStrs.get(4) + "," + tmpStrs.get(5) + ","
+ tmpStrs.get(6));
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}

利用java从docx文档中提取文本内容的更多相关文章

  1. 如何使用免费PDF控件从PDF文档中提取文本和图片

             如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...

  2. MVC架构下,使用NPOI读取.DOCX文档中表格的内容

    1.使用NPOI,可以在没有安装office的设备上读wiod.office.2.本文只能读取.docx后缀的文档.3.MVC架构中,上传文件只能使用form表单提交,转到控制器后要依次实现文件上传. ...

  3. 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  4. 【python】使用HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies

    一.从HTML文档中提取链接 模块HTMLParser,该模块使我们能够根据HTML文档中的标签来简洁.高效地解析HTML文档. 处理HTML文档的时候,我们常常需要从其中提取出所有的链接.使用HTM ...

  5. Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  6. python 解析docx文档的方法,以及利用Python从docx文档提取插入的文本对象和图片

    首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...

  7. java使用正则从爬虫爬的txt文档中提取QQ邮箱

    我的需求是从一堆文档中提取出qq邮箱,写了这篇帖子,希望能帮助和我有一样需求的人,谢谢!...... import java.io.BufferedReader; import java.io.Fil ...

  8. Java 在 Word 文档中使用新文本替换指定文本

    创作一份文案,经常会高频率地使用某些词汇,如地名.人名.人物职位等,若表述有误,就需要整体撤换.文本将介绍如何使用Spire.Doc for Java,在Java程序中对Word文档中的指定文本进行替 ...

  9. Java 在PDF文档中绘制图形

    本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...

随机推荐

  1. python—day02_基本数据类型

    1,字符串 字符串常用功能: 移除空白 分割 长度 索引 切片 1)移除空白 """S.strip([chars]) -> str Return a copy of ...

  2. Gym100548F Color

    题目链接:https://vjudge.net/problem/Gym-100548F 题目大意: n 朵花,按顺序排成一排.从 m 种颜色中选出 k 种颜色,给这 n 朵花染色,要求相邻的花颜色不同 ...

  3. 【Mac】anaconda自定义channels

    1.查看channels conda config --show channels 输出 channels: - defaults 2.添加channels conda config --add ch ...

  4. Java——DOS命令窗口用命令编译文件夹下所有.java文件

    1.进入指定目录    cd 进入用户主目录    cd ~ 进入用户主目录     cd - 返回进入此目录之前所在的目录     cd .. 返回上级目录    cd\ 直接退回到当前盘根目录2. ...

  5. PhpStorm2016.3激活

    选择License server,输入以下任意一个地址: http://idea.imsxm.com/http://114.215.133.70:41017/http://mcpmcc.com:101 ...

  6. Channels集成到Django消息实时推送

    channel架构图 InterFace Server:负责对协议进行解析,将不同的协议分发到不同的Channel Channel Layer:频道层,可以是一个FIFO队列,通常使用Redis Dj ...

  7. 飞机调度 Now or Later? LA 3211 (2-SAT问题)

    洛谷题目传送门 题目描述 有n架飞机需要着陆.每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种.第i架飞机的早着陆时间为Ei,晚着陆时间为Li,不得在其他时间着陆.你的任务是为这些 ...

  8. [Objective-C] 006_Protocol(协议)

    学过java的同学都知道Interface(接口),那么在Objective-C中有没有接口呢?其实 Objective-C中用Protocol(协议)来实现的,在Objective-C具体怎么用,我 ...

  9. This关键字练习

    Account: package com.aff.ex; public class Account { private int id;// 账号 private double balance;// 余 ...

  10. python requests用于测试

    https://blog.csdn.net/niedongri/article/details/71404314 https://blog.csdn.net/temanm/article/detail ...