利用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文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...
随机推荐
- tcp/ip 学习笔记zz
http://blog.csdn.net/goodboy1881/category/204448.aspx 坚持看!
- 检查可执行App类型是否为executable (腾讯上线预审核报错)otool工具使用
https://blog.csdn.net/lovechris00/article/details/81561627 查看IPA文件的路径 1,解压缩 xcode导出的xxx.ipa文件 2,然后在解 ...
- hdu2093 考试排名(还需完善)
下面代码是借鉴的.好多的知识点等着完善 #include <iostream> #include <string> #include <algorithm> usi ...
- Istio VirtualService 虚拟服务
概念及示例 VirtualService 描述了一个或多个用户可寻址目标到网格内实际工作负载之间的映射 . 虚拟服务让您配置如何在服务网格内将请求路由到服务,这基于 Istio 和平台提供的基本的连通 ...
- UIAutomator2的API文档(三)
1.UI对象识别器Selector 用法d(text='Clock', className='android.widget.TextView') 支持以下参数,详细信息可参考UiSelector Ja ...
- 二维DCT变换 | Python实现
引言 最近专业课在学信息隐藏与数字水印,上到了变换域隐藏技术,提到了其中的DCT变换,遂布置了一个巨烦人的作业,让手动给两个\(8\times8\)的矩阵做二维DCT变换,在苦逼的算了一小时后,我决定 ...
- Ratel源码-C/S事件梳理
一.Ratel介绍 Ratel 是一个可以在命令行中玩斗地主的项目,可以使用小巧的jar包在拥有JVM环境的终端中进行游戏,同时支持人人对战和人机对战两种模式,丰富你的空闲时间! 二.玩法Demo 三 ...
- Bitwarden_rs搭建
最近LastPass网络极其不稳定,正好闲下来找到了Bitwarden_rs这个替代品,感觉不错,分享记录下部署过程. 一.Docker方式部署 #获取镜像 docker pull bitwarden ...
- Rocket - util - HeterogeneousBag
https://mp.weixin.qq.com/s/5hNM4yeQjaLvAJzgMG9PGQ 介绍HeterogeneousBag的实现. 1. 基本介绍 一个口袋(bag ...
- (Java实现) 美元汇率
时空限制 1000ms/128MB 题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后 ...