Hi,
This is the simple code written using Java to find no. of slides in the presentationML document.
package pptxslidescnt; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import tozipandunzip.ToUnZipFile;
public class PptxSlidesCnt { public static void main(String [] args){ int slidesCnt = 0; // Folder in which output can be found. This value is hard coded, but can also be obtained dynamically String extractToFolder = "E:\\ooxml\\pptslidescount\\count"; // Path of Presentation file String fromToFolder = "E:\\ooxml\\pptslidescount\\slidecount.pptx";
String presentationXmlName = null; String pathpresentationXmlName = null; ToUnZipFile toUnZipFile = null; DocumentBuilderFactory docBuilderFactory = null; DocumentBuilder docBuilder = null; File contentTypeXML = null; Document contentTypeXMLdoc = null; NodeList overrideLst = null; Node overRideNode = null; NamedNodeMap nnmap = null; Document presentationXMLDoc = null; NodeList slidesLst = null; try{ toUnZipFile = new ToUnZipFile();
//The file has been unzipped toUnZipFile.unZipFile(fromToFolder,extractToFolder);
//Obtain a new instance of a DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
//Creates a new instance of a DocumentBuilder using the currently configured parameters docBuilder = docBuilderFactory.newDocumentBuilder(); contentTypeXML = new File(extractToFolder + "\\" + "[Content_Types].xml");
//parse an XML file and get any element's content or attribute's value WITHOUT "walking the tree" contentTypeXMLdoc = docBuilder.parse (contentTypeXML);
//retrieve all Override elements in the [Content_Types].xml overrideLst = contentTypeXMLdoc.getElementsByTagName("Override"); for(int overrideLstCnt=0; overrideLstCnt<overrideLst.getLength() ; overrideLstCnt++){ overRideNode = overrideLst.item(overrideLstCnt); nnmap = overRideNode.getAttributes(); if
(nnmap.getNamedItem("ContentType").toString().contains("vnd.openxmlformats-officedocument.presentationml.presentatio
n.main+xml")) { presentationXmlName =nnmap.getNamedItem("PartName").getNodeValue(); pathpresentationXmlName = extractToFolder+presentationXmlName; presentationXMLDoc = docBuilder.parse (pathpresentationXmlName); slidesLst = presentationXMLDoc.getElementsByTagName("p:sldId"); slidesCnt = slidesLst.getLength(); break; } }//end of for loop System.out.println("No of slides = "+slidesCnt); }catch (SAXParseException err) { System.out.println(" " + err.getMessage ()); }catch (Throwable t) { t.printStackTrace (); } } } Code to unzip a zip file: public static void unZipFile(String zipFileName, String toExtractFile) { String inputFileName = zipFileName; String desFileName = toExtractFile; try { File sourceZipFile = new File(inputFileName); File destDirectory = new File(desFileName); ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); Enumeration enumeration = zipFile.entries(); while(enumeration.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); String currName = zipEntry.getName(); File destFile = new File(destDirectory, currName); File destinationParent = destFile.getParentFile(); destinationParent.mkdirs(); if( ! zipEntry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(zipEntry)); int currentByte; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos); while((currentByte = is.read()) != -1) { dest.write(currentByte); } dest.flush(); dest.close(); is.close(); } } } catch(Exception e) { System.out.println(" Exception "+e); }}
Sheela