Search This Blog

Translate

Showing posts with label apache poi. Show all posts
Showing posts with label apache poi. Show all posts

Thursday, May 16, 2013

Apache POI : Parse text from .docx, .pptx, .xlsx file using Apache POI 3.9

Get real time news update from your favorite websites.
Don't miss any news about your favorite topic.
Personalize your app.

Check out NTyles.


Get it on....

NTyles-App


In past when I first created this blog and blogged in "Parse text from word files", Apache Poi package was not able to parse texts from .docx, .xlsx and .pptx files. And now it can extract text from .docx, .pptx, .xlsx files.

Here's the code on how to parse text from those files using Apache Poi 3.9 .


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blogspot.computergodzilla.xparser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
import org.xml.sax.SAXException;

/**
 * This class parses the .docx ,.pptx and .xlsx files.
 *
 * @author Mubin Shrestha
 */
public class XParsers {

    /**
     * This method parses the .docx files.
     *
     * @param docx
     * @throws FileNotFoundException
     * @throws IOException
     * @throws XmlException
     * @throws InvalidFormatException
     * @throws OpenXML4JException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    public void DocFileContentParser(OPCPackage docx) throws FileNotFoundException,
            IOException,
            XmlException,
            InvalidFormatException,
            OpenXML4JException,
            ParserConfigurationException,
            SAXException {
        XWPFWordExtractor xw = new XWPFWordExtractor(docx);
        System.out.println(xw.getText());
    }

    /**
     * This method parses the pptx files
     *
     * @param pptx
     * @throws FileNotFoundException
     * @throws IOException
     * @throws InvalidFormatException
     * @throws XmlException
     * @throws OpenXML4JException
     */
    public void ppFileContentParser(OPCPackage pptx) throws FileNotFoundException,
            IOException,
            InvalidFormatException,
            XmlException,
            OpenXML4JException {
        XSLFPowerPointExtractor xw = new XSLFPowerPointExtractor(pptx);
        System.out.println(xw.getText());
    }

    /**
     * This method parsed xlsx files
     *
     * @param xlsx
     * @throws FileNotFoundException
     * @throws IOException
     * @throws InvalidFormatException
     * @throws XmlException
     * @throws OpenXML4JException
     */
    public void excelContentParser(OPCPackage xlsx) throws FileNotFoundException,
            IOException,
            InvalidFormatException,
            XmlException,
            OpenXML4JException {
        XSSFExcelExtractor xe = new XSSFExcelExtractor(xlsx);
        System.out.println(xe.getText());
    }

    /**
     * main method
     *
     * @param args
     * @throws FileNotFoundException
     * @throws IOException
     * @throws XmlException
     * @throws InvalidFormatException
     * @throws OpenXML4JException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    public static void main(String args[]) throws FileNotFoundException,
            IOException,
            XmlException,
            InvalidFormatException,
            OpenXML4JException,
            ParserConfigurationException,
            SAXException {
        File file = new File("fileName"); //give your file name here of 
                                          //which you want to parse text
        FileInputStream fs = new FileInputStream(file);
        OPCPackage d = OPCPackage.open(fs);
        XParsers xp = new XParsers();
        if (file.getName().endsWith(".docx")) {
            xp.DocFileContentParser(d);
        } else if (file.getName().endsWith(".xlsx")) {
            xp.excelContentParser(d);
        } else if (file.getName().endsWith(".pptx")) {
            xp.ppFileContentParser(d);
        }
    }
}


Edit:

Well I must be a very bad blogger. Here is the picture on the list of libraries needed:


Sunday, December 30, 2012

Apache Lucene--How to index .doc and .pdf files?

Want to follow news you care about.
Don't want to miss any action from premier League, Spanish League and other Leagues.
Want to make app with your own layout.

Check out NTyles.

Get it on....

NTyles-App


In my previous blog I show you guys how to index text files. Some of you may be thinking "Why the heck this guy index only text files? Why not .doc and .pdf files?". So this post is dedicated for those who are wondering about how to and why he didn't. The overall mechanism of how to index .doc , .pdf files will be presented in three series. Before we really move on to the real topic first let me make you'al clear that Apache Lucene is able to index texts only so we first have to parse texts from unsupported files (.doc, .pdf, .xls and etc). So now in upcoming posts we will parse texts from .doc file and in second post we will parse .pdf files and finally the third post will be to index both .doc and .pdf files. Well gonna be long!! Be ready.