【问题标题】:How to use Apache HWPF to extract text and images out of a DOC file如何使用 Apache HWPF 从 DOC 文件中提取文本和图像
【发布时间】:2009-03-12 05:06:36
【问题描述】:

我下载了Apache HWPF。我想用它来读取 doc 文件并将其文本写入纯文本文件。我不太了解 HWPF。

我的非常简单的程序在这里:

我现在有 3 个问题:

  1. 一些包有错误(他们找不到 apache hdf)。我该如何解决?

  2. 如何使用 HWDF 的方法来查找和提取图像?

  3. 我的程序的某些部分不完整且不正确。所以请帮我完成它。

我必须在 2 天内完成这个项目。

我再重复一遍,请帮我完成这个。

非常感谢你们的帮助!!!

这是我的基本代码:

public class test {
  public void m1 (){
    String filesname = "Hello.doc";
    POIFSFileSystem fs = null;
    fs = new POIFSFileSystem(new FileInputStream(filesname ); 
    HWPFDocument doc = new HWPFDocument(fs);
    WordExtractor we = new WordExtractor(doc);
    String str = we.getText() ;
    String[] paragraphs = we.getParagraphText();
    Picture pic = new Picture(. . .) ;
    pic.writeImageContent( . . . ) ;
    PicturesTable picTable = new PicturesTable( . . . ) ;
    if ( picTable.hasPicture( . . . ) ){
      picTable.extractPicture(..., ...);
      picTable.getAllPictures() ;
    }
}

【问题讨论】:

标签: java apache-poi hwpf


【解决方案1】:

Apache Tika 会为你做这件事。它处理与 POI 的对话以执行 HWPF 工作,并为您提供文件内容的 XHTML 或纯文本。如果你注册了一个递归解析器,那么你也会得到所有嵌入的图像。

【讨论】:

    【解决方案2】:
        //you can use the org.apache.poi.hwpf.extractor.WordExtractor to get the text
        String fileName = "example.doc";
        HWPFDocument wordDoc = new HWPFDocument(new FileInputStream(fileName));
        WordExtractor extractor = new WordExtractor(wordDoc);
        String[] text = extractor.getParagraphText();
        int lineCounter = text.length;
        String articleStr = ""; // This string object use to store text from the word document.
        for(int index = 0;index < lineCounter;++ index){
            String paragraphStr = text[index].replaceAll("\r\n","").replaceAll("\n","").trim();
            int paragraphLength = paragraphStr.length();
            if(paragraphLength != 0){
                articleStr.concat(paragraphStr);
            }
        }
        //you can use the org.apache.poi.hwpf.usermodel.Picture to get the image
        List<Picture> picturesList = wordDoc.getPicturesTable().getAllPictures();
        for(int i = 0;i < picturesList.size();++i){
            BufferedImage image = null;
            Picture pic = picturesList.get(i);
            image = ImageIO.read(new ByteArrayInputStream(pic.getContent()));
            if(image != null){
                System.out.println("Image["+i+"]"+" ImageWidth:"+image.getWidth()+" ImageHeight:"+image.getHeight()+" Suggest Image Format:"+pic.suggestFileExtension());
            }
        }
    

    【讨论】:

      【解决方案3】:

      如果你只想这样做,而不关心编码,你可以使用Antiword

      $ antiword file.doc > out.txt

      【讨论】:

        【解决方案4】:

        事后很久我就知道了,但我在 google 代码上找到了 TextMining,它更准确且非常易于使用。然而,它几乎是废弃的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-06
          • 2011-07-29
          相关资源
          最近更新 更多