Last active
November 2, 2022 19:39
-
-
Save vijaypatidar/74f3b93c28bdc4693e52612c4d2d17a5 to your computer and use it in GitHub Desktop.
Extract images from PPTX using apache poi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'java' | |
} | |
group 'com.vkpapps' | |
version '1.0-SNAPSHOT' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
implementation group: 'org.apache.poi', name: 'poi', version: '5.2.2' | |
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.2' | |
... | |
} | |
test { | |
useJUnitPlatform() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.List; | |
import org.apache.poi.xslf.usermodel.XMLSlideShow; | |
import org.apache.poi.xslf.usermodel.XSLFPictureData; | |
public class ImageExtracter { | |
/* | |
This will extarct all image from PPTX and save them in images folder | |
*/ | |
public void extract(XMLSlideShow slideShow){ | |
List<XSLFPictureData> pictures = slideShow.getPictureData(); | |
File parent = new File("images"); | |
if (!parent.exists())parent.mkdir(); | |
pictures.forEach(pictureData->{ | |
try{ | |
String name = pictureData.getFileName(); | |
FileOutputStream outputStream = new FileOutputStream(new File(parent,name)); | |
outputStream.write(pictureData.getData()); | |
outputStream.flush(); | |
outputStream.close(); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.List; | |
import org.apache.poi.xslf.usermodel.XMLSlideShow; | |
import org.apache.poi.xslf.usermodel.XSLFPictureData; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
File file = new File("demo.pptx"); | |
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); | |
// extracting all images | |
new ImageExtracter().getImage(ppt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment