当前位置:   article > 正文

Java Spire.Presentation 之PPT文本图片内容提取

spire.presentation

前言

在这里插入图片描述

应公司需求,需实现以下功能

  1. PPT文本内容的替换;
  2. PPT文本内容的提取;
  3. PPT中图片的提取存放;

此文章将使用Spire.Presentation实现对PPT文件中文本内容及图片的提取;

Spire.Presentation for Java是一个专业的 PowerPoint API,180846090使开发人员能够在 Java 应用程序中创建、读取、编写、转换和保存 PowerPoint 文档。作为一个独立的Java 库,Spire.Presentation 不需要在系统上安装Microsoft PowerPoint。

文档准备

小编准备了以下两个文件:《ppt_demo.ppt》《pptx_demo.pptx》,分别代表不同版本的PPT,以便提取测试,如下图在这里插入图片描述

引入Maven依赖

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
	<dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>4.9.2</version>
    </dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

代码块

package com.bjzaxk.utils;

import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.ParagraphEx;
import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;

public class Demo {
    public static void main(String[] args) {
//        String filePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\ppt_demo.ppt";
        String filePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\pptx_demo.pptx";
        // 文本提取后存放路径及文件名
//        String extractFilePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\ppt_demo.txt";
        String extractFilePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\pptx_demo.txt";
        // 图片提取后存放路径
        String imageFilePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\";

        pptTextExtract(filePath, extractFilePath);
        pptImageExtract(filePath, imageFilePath);
    }

    /**
     * @description: 提取PPT中的文本信息
     * @author: Mr.Jkx
     * @time: 2023/2/2 14:53
     */
    public static void pptTextExtract(String filePath, String extractFilePath) {
        try {
            //加载文档
            Presentation ppt = new Presentation();
            ppt.loadFromFile(filePath);
            StringBuilder buffer = new StringBuilder();
            //遍历文档中的幻灯片,提取文本
            for (Object slide : ppt.getSlides()) {
                for (Object shape : ((ISlide) slide).getShapes()) {
                    if (shape instanceof IAutoShape) {
                        for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
                            buffer.append(((ParagraphEx) tp).getText()).append("\r\n");
                        }
                    }
                }
            }
            if (buffer.length() > 0) {
                //保存到文本文件
                FileWriter writer = new FileWriter(extractFilePath);
                writer.write(buffer.toString());
                writer.flush();
                writer.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description: 提取PPT中的图片
     * @author: Mr.Jkx
     * @time: 2023/1/10 14:26
     */
    public static void pptImageExtract(String filePath, String imageFilePath) {
        try {
            //加载文档
            Presentation ppt = new Presentation();
            ppt.loadFromFile(filePath);
            //提取文档中的所有图片
            for (int i = 0; i < ppt.getImages().getCount(); i++) {
                BufferedImage image = ppt.getImages().get(i).getImage();
                ImageIO.write(image, "PNG", new File(imageFilePath + "pptImage_" + System.currentTimeMillis() + ".png"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

提取结果验证

  • 提取文本会存放于一个txt文件中(小编工作需求,其他类型文件未尝试!);
  • 提取的图片会存放于“imageFilePath ”路径中;
  • 提取过程中不会改变原本PPT内容;
ppt_demo.ppt 提取结果

在这里插入图片描述

pptx_demo.pptx 提取结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/728370
推荐阅读
相关标签
  

闽ICP备14008679号