word文档实现标题提取

word文档实现标题提取

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

java 复制代码
    /**
     * 标题提取
     * @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方法:

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

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

java 复制代码
		<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>
相关推荐
weixin_568996064 分钟前
Vue.js生命周期destroyed钩子中内存泄漏排查与资源释放
jvm·数据库·python
Full Stack Developme8 分钟前
Hutool EnumUtil 教程
开发语言·windows·python
baidu_3409988211 分钟前
PySpark中高效展开嵌套数组:避免笛卡尔爆炸的正确实践.txt
jvm·数据库·python
TechWayfarer14 分钟前
IP定位API接口怎么选?在线、离线还是混合?一套决策框架
网络·python·tcp/ip
weixin_5806140017 分钟前
如何自动同步SQL多语言字段_通过触发器实现国际化更新
jvm·数据库·python
csdn_aspnet17 分钟前
告别重复造轮子:Codex写脚本,运维/DevOps场景下,用Codex批量生成Shell/Python自动化脚本
运维·python·ai·自动化·devops·codex·辅助编程
likerhood21 分钟前
Java 中的 `clone()` 与 `Cloneable` 接口详解
java·开发语言·python
2401_8371638922 分钟前
SQL中窗口函数使用注意事项_避免潜在的数据陷阱
jvm·数据库·python
m0_7349497923 分钟前
mysql数据库性能调优的常用指标有哪些_深入理解QPS与TPS
jvm·数据库·python
前端技术23 分钟前
华为余承东:鸿蒙终端设备数突破5500万
java·前端·javascript·人工智能·python·华为·harmonyos