java,poi,提取ppt文件中的文字内容

注意,不涉及图片处理。

先上pom依赖:

        <!-- 处理PPTX文件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>
        <!-- 处理PPT文件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>5.2.3</version>
        </dependency>

代码:

public static void main(String[] args) {
        String filePath = "C:\\xx.pptx"; // 待处理ppt全路径
        try {
            IOUtils.setByteArrayMaxOverride(160000000);//分配内存160M
            String content = readPresentation(filePath);
            System.out.println(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readPresentation(String filePath) throws Exception {
        if (filePath.toLowerCase().endsWith(".pptx")) {
            return readPPTX(filePath);
        } else if (filePath.toLowerCase().endsWith(".ppt")) {
            return readPPT(filePath);
        }
        throw new IllegalArgumentException("Unsupported file format");
    }

    // 处理PPTX文件
    private static String readPPTX(String filePath) throws Exception {
        StringBuilder content = new StringBuilder();
        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filePath));

            for (XSLFSlide slide : ppt.getSlides()) {
                for (XSLFShape shape : slide.getShapes()) {
                    if (shape instanceof XSLFTextShape) {
                        content.append(((XSLFTextShape) shape).getText()).append("\n");
                    }
                }
            }

        return content.toString();
    }

    // 处理PPT文件
    private static String readPPT(String filePath) throws Exception {
        StringBuilder content = new StringBuilder();
        try (HSLFSlideShow ppt = new HSLFSlideShow(new FileInputStream(filePath))) {
            for (HSLFSlide slide : ppt.getSlides()) {
                // 读取幻灯片中的形状
                for (HSLFShape shape : slide.getShapes()) {
                    if (shape instanceof HSLFTextShape) {
                        HSLFTextShape textShape = (HSLFTextShape) shape;
                        content.append(textShape.getText()).append("\n");
                    }
                }
                // 读取幻灯片中的文本框(兼容旧版本)
                for (List<HSLFTextParagraph> textParagraphs : slide.getTextParagraphs()) {
                    for (HSLFTextParagraph para : textParagraphs) {
                        content.append(para).append("\n");
                    }
                }
            }
        }
        return content.toString();
    }

 最终效果与wps自带的ppt转word只勾选文本差不多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值