注意,不涉及圖片處理。
先上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);//分配內存160MString 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只勾選文本差不多。