利用java从docx文档中提取文本内容
利用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文档中提取文本内容的更多相关文章
- 如何使用免费PDF控件从PDF文档中提取文本和图片
如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...
- MVC架构下,使用NPOI读取.DOCX文档中表格的内容
1.使用NPOI,可以在没有安装office的设备上读wiod.office.2.本文只能读取.docx后缀的文档.3.MVC架构中,上传文件只能使用form表单提交,转到控制器后要依次实现文件上传. ...
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)
对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...
- 【python】使用HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies
一.从HTML文档中提取链接 模块HTMLParser,该模块使我们能够根据HTML文档中的标签来简洁.高效地解析HTML文档. 处理HTML文档的时候,我们常常需要从其中提取出所有的链接.使用HTM ...
- Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...
- python 解析docx文档的方法,以及利用Python从docx文档提取插入的文本对象和图片
首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...
- java使用正则从爬虫爬的txt文档中提取QQ邮箱
我的需求是从一堆文档中提取出qq邮箱,写了这篇帖子,希望能帮助和我有一样需求的人,谢谢!...... import java.io.BufferedReader; import java.io.Fil ...
- Java 在 Word 文档中使用新文本替换指定文本
创作一份文案,经常会高频率地使用某些词汇,如地名.人名.人物职位等,若表述有误,就需要整体撤换.文本将介绍如何使用Spire.Doc for Java,在Java程序中对Word文档中的指定文本进行替 ...
- Java 在PDF文档中绘制图形
本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...
随机推荐
- 00008 - layui 表单验证,需要验证,但非必输
当使用layui的验证规则,比如 手机, <input type="text" name="userName" lay-verify="phon ...
- python3.x 基础一:str字符串方法
*字符串不能更改值 数据类型字符串str | capitalize(...) 返回字符串中第一个字母大写 | S.capitalize() -> str | | ...
- CF #459 D. MADMAX
D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Understanding REST and RESTful APIs
Understanding REST and RESTful APIs If you've spent any amount of time with modern web development, ...
- HTML特殊符号整理
往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ...
- Vue中计算属性(computed)和监听属性函数watch的比较
1.计算属性适用的情形我们可能会有这样的需求,一个数据属性在它所依赖的属性发生变化时,也要发生变化,这种情况下,我们最好使用计算属性.例如在下面这个例子中,如果我们使用监听函数,代码就会变得有点冗余. ...
- Verilog代码和FPGA硬件的映射关系(三)
组合逻辑和FPGA之间的映射关系我们知道了,那时序逻辑和FPGA之间又是一种怎样的映射关系呢?我们就以前面寄存器章节的例子来向大家说明,也一同把当时为什么用异步复位更节约资源的原因告诉大家.我们先来看 ...
- 第7章 PCA与梯度上升法
主成分分析法:主要作用是降维 疑似右侧比较好? 第三种降维方式: 问题:????? 方差:描述样本整体分布的疏密的指标,方差越大,样本之间越稀疏:越小,越密集 第一步: 总结: 问题:????怎样使其 ...
- spring框架中三层架构相关的注解
做了这么多年的C++,再去学Java,确实发现,语言都是相通的,即使是Java的那么多生态,理解起来也并不费劲 Spring 框架目前还在学习中,处于 Tourist 阶段,目前只求会做,不求原理,等 ...
- 01 . Tomcat简介及部署
Tomcat简介 Tomcat背景 tomcat就是常用的的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中.而tomcat本 ...