Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created March 25, 2015 21:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/ff56d302edf4ebaa099f to your computer and use it in GitHub Desktop.
Save adamloving/ff56d302edf4ebaa099f to your computer and use it in GitHub Desktop.
Read PowerPoint ppt pptx using Apache Poi in java!
import java.io.*;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.POIXMLProperties.*;
import org.apache.poi.xslf.usermodel.*;
public class HelloPoi {
public static void main(String[] args) {
String fileName;
if (args.length > 0) {
fileName = args[0];
} else {
System.out.println("No file name specified.");
return;
}
FileInputStream inputStream;
try {
inputStream = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
XMLSlideShow ppt;
try {
ppt = new XMLSlideShow(inputStream);
} catch (IOException e) {
e.printStackTrace();
return;
}
readPPT(ppt);
}
public static void readPPT(XMLSlideShow ppt) {
CoreProperties props = ppt.getProperties().getCoreProperties();
String title = props.getTitle();
System.out.println("Title: " + title);
for (XSLFSlide slide: ppt.getSlides()) {
System.out.println("Starting slide...");
XSLFShape[] shapes = slide.getShapes();
for (XSLFShape shape: shapes) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape)shape;
String text = textShape.getText();
System.out.println("Text: " + text);
}
}
}
}
}
@adamloving
Copy link
Author

To run

  • download POI jar files
  • Download Eclipse (or use $ javac -cp ... and $ java -cp ... specifying jar files)
  • right click on project and add external jars (be sure to grab sub folders)
  • edit run configuration to pass command line argument

@SaharNasiri
Copy link

Hi, thanks for the great sample.
Could please help me to get font name, of text of each slides?

@shqear93
Copy link

thanks a lot

Copy link

ghost commented Sep 18, 2018

Hi Do you have reference for how to embed font inside .pptx file

@Meowcenary
Copy link

Very helpful, really appreciate you sharing this.

@sjethvani
Copy link

Thanks adam for writing this & showing sample code to work with apache poi ppt files.
I am right now working on writing simple prog to compare two ppt files . I have used apache poi & was able to fetch shapes of slide using getShapes() . Now since my ppt contains images , shape is of XSLFPictureShape type. Can you tell me how should I compare these XSLFPictureShape objects .

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment