word文档实现标题提取

embedded/2024/10/20 21:01:50/

word文档实现标题提取


话不多说,直接上代码(使用的是com.aspose.words.*下所有的包,最后附依赖jar包,解压zip文件,用里面的jar包就行,也可以自己maven下载)

    /*** 标题提取* @param inputFilePath* @param outputFilePath* @return*/public static void modifyWordDocument(String inputFilePath, String outputFilePath) {try {// 加载 Word 文档Document doc = new Document(inputFilePath);// 遍历文档的节点for (Object node : doc.getChildNodes(NodeType.PARAGRAPH, true)) {Paragraph paragraph = (Paragraph) node;// 检查段落的样式是否为标题样式if (paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_1 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_2 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_3 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_4 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_5 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_6 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_7 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_8 &&paragraph.getParagraphFormat().getStyle().getStyleIdentifier() != StyleIdentifier.HEADING_9) {// 如果不是标题样式,则删除该段落paragraph.remove();}}// 删除所有表格for (Object node : doc.getChildNodes(NodeType.TABLE, true)) {Table table = (Table) node;table.remove();}// 用于存储要删除的章节List<Section> emptySections = new ArrayList<>();// 遍历文档的章节for (Section section : doc.getSections()) {boolean isEmpty = true;// 检查每个章节中的所有段落for (Object node : section.getChildNodes(NodeType.PARAGRAPH, true)) {Paragraph paragraph = (Paragraph) node;// 如果找到非空段落,则该章节不为空if (!paragraph.getRange().getText().trim().isEmpty()) {isEmpty = false;break;}}// 如果章节是空的,加入删除列表if (isEmpty) {emptySections.add(section);}}// 删除存储的空章节for (Section section : emptySections) {if (section.getParentNode() != null) {// 只有在有父节点时才删除section.remove();}}// 保存修改后的文档doc.save(outputFilePath);} catch (Exception e) {e.printStackTrace();}}

下面是测试main方法:

    public static void main(String[] args) {String inputFilePath = "your\\file_path\\test.docx";String outputFilePath = "your\\file_path\\test-标题提取.docx";modifyWordDocument(inputFilePath, outputFilePath);}

pom文件中依赖引入,我引得是本地包

		<dependency><groupId>com.aspose-word-cracked</groupId><artifactId>aspose-word-cracked</artifactId><scope>system</scope><version>1.0</version><systemPath>${basedir}/libs/aspose-words-20.12-jdk17-crack.jar</systemPath></dependency>

http://www.ppmy.cn/embedded/129085.html

相关文章

SpringMVC源码-接口请求执行流程,包含九大内置组件的实例化初始化,拦截器调用,页面渲染等源码讲解

一、上传文件功能的实现: 前端JSP代码: form 表单提交&#xff0c;enctype为multipart/form-data&#xff0c;请求方式POST <% page contentType"text/html;charsetUTF-8" language"java" %> <%pageContext.setAttribute("ctx",reque…

《TH-OCR:强大的光学字符识别技术》

在当今数字化的时代&#xff0c;高效准确地将纸质文档、图片中的文字转换为可编辑的电子文本至关重要。而 TH-OCR&#xff08;清华 OCR&#xff09;就是一款在光学字符识别领域表现卓越的软件。 一、TH-OCR 的简介 TH-OCR 是由清华大学电子工程系智能图文信息处理研究室研发的光…

【工具变量】上市公司企业大数据应用数据、大数据应用指数(2001-2023年)

一、测算方式&#xff1a; 参考顶刊《经济研究》张叶青&#xff08;2021&#xff09;老师的做法&#xff0c;本文利用关键词在公司年报中出现的次数来度量公司的大数据应用程度。关键词的选取借鉴了以往文献 、政府文件以及业界报告等。我们一方面紧扣大数据的定义; 另一方面则…

100多种【基于YOLOv8/v10/v11的目标检测系统】目录(python+pyside6界面+系统源码+可训练的数据集+也完成的训练模型)

待更新(持续更新&#xff09;&#xff0c;早关注&#xff0c;不迷路............................................................................... 基于YOLOv8的车辆行人实时检测系统基于YOLOv10的车辆行人实时检测系统基于YOLOv11的车辆行人实时检测系统基于YOLOv8的农…

JavaScript 中的定时器与动画基础

setInterval 和 setTimeout 都是 JavaScript 中的定时器函数&#xff0c;用于在一定的时间间隔后执行函数。 setInterval 函数用于按照指定的时间间隔重复执行一个函数。它接受两个参数&#xff0c;第一个参数是要执行的函数&#xff0c;第二个参数是时间间隔的毫秒数。使用示…

什么是堡垒机 ?安全为什么需要堡垒 ?

堡垒机在企业安全防护中扮演着核心角色&#xff0c;通过集中控制访问权限、实时监控操作行为、提供详细审计日志&#xff0c;有效隔离外部风险&#xff0c;保障内部资源安全&#xff0c;是确保企业网络和数据安全的重要防线。 一、什么是堡垒机 堡垒机&#xff0c;也被称为跳…

netty编程之对reactor的应用

写在前面 在netty使用了reactor的线程模型&#xff08;或者叫做工作模式&#xff09;。本文就一起来看下其是如何使用的。 1&#xff1a;不同的rector对应的不同的编码方式 首先是rector的单线程模型&#xff0c;对应到netty中的编码方式如下&#xff1a; // 这里的1&#…

Java基础08-集合框架—单列集合

一、集合框架 二、集合框架—单列集合 1、Collection 集合体系 Collection是单列集合的祖宗&#xff0c;它规定的方法(功能)是全部单列集合都会继承的。 Collection集合特点&#xff1a; List系列集合&#xff1a;添加的元素是有序、可重复、有索引。 ArrayList、LinekdList &…