apache-tika从ppt-pdf-xls读取文本

简介: apache-tika从ppt-pdf-xls读取文本


你若要喜爱自己的价值,你就得给世界创造价值。——歌德

代码仓库:

GitHub - apache/tika: The Apache Tika toolkit detects and extracts metadata and text from over a thousand different file types (such as PPT, XLS, and PDF).

官网:

https://tika.apache.org/

快速开始:

Apache Tika – Getting Started with Apache Tika

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tika.example;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.xml.sax.ContentHandler;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.langdetect.optimaize.OptimaizeLangDetector;
import org.apache.tika.language.detect.LanguageDetector;
import org.apache.tika.language.detect.LanguageResult;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
/**
 * Demonstrates how to call the different components within Tika: its
 * {@link Detector} framework (aka MIME identification and repository), its
 * {@link Parser} interface, its {@link org.apache.tika.language.LanguageIdentifier} and other goodies.
 * <p>
 * It also shows the "easy way" via {@link AutoDetectParser}
 */
public class MyFirstTika {
    public static void main(String[] args) throws Exception {
        String filename = args[0];
        TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
        Metadata metadata = new Metadata();
        String text = parseUsingComponents(filename, tikaConfig, metadata);
        System.out.println("Parsed Metadata: ");
        System.out.println(metadata);
        System.out.println("Parsed Text: ");
        System.out.println(text);
        System.out.println("-------------------------");
        metadata = new Metadata();
        text = parseUsingAutoDetect(filename, tikaConfig, metadata);
        System.out.println("Parsed Metadata: ");
        System.out.println(metadata);
        System.out.println("Parsed Text: ");
        System.out.println(text);
    }
    public static String parseUsingAutoDetect(String filename, TikaConfig tikaConfig,
                                              Metadata metadata) throws Exception {
        System.out.println("Handling using AutoDetectParser: [" + filename + "]");
        AutoDetectParser parser = new AutoDetectParser(tikaConfig);
        ContentHandler handler = new BodyContentHandler();
        TikaInputStream stream = TikaInputStream.get(new File(filename), metadata);
        parser.parse(stream, handler, metadata, new ParseContext());
        return handler.toString();
    }
    public static String parseUsingComponents(String filename, TikaConfig tikaConfig,
                                              Metadata metadata) throws Exception {
        MimeTypes mimeRegistry = tikaConfig.getMimeRepository();
        System.out.println("Examining: [" + filename + "]");
        metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, filename);
        System.out.println(
                "The MIME type (based on filename) is: [" + mimeRegistry.detect(null, metadata) +
                        "]");
        InputStream stream = TikaInputStream.get(new File(filename));
        System.out.println(
                "The MIME type (based on MAGIC) is: [" + mimeRegistry.detect(stream, metadata) +
                        "]");
        stream = TikaInputStream.get(new File(filename));
        Detector detector = tikaConfig.getDetector();
        System.out.println("The MIME type (based on the Detector interface) is: [" +
                detector.detect(stream, metadata) + "]");
        LanguageDetector langDetector = new OptimaizeLangDetector().loadModels();
        LanguageResult lang =
                langDetector.detect(FileUtils.readFileToString(new File(filename), UTF_8));
        System.out.println("The language of this content is: [" + lang.getLanguage() + "]");
        // Get a non-detecting parser that handles all the types it can
        Parser parser = tikaConfig.getParser();
        // Tell it what we think the content is
        MediaType type = detector.detect(stream, metadata);
        metadata.set(Metadata.CONTENT_TYPE, type.toString());
        // Have the file parsed to get the content and metadata
        ContentHandler handler = new BodyContentHandler();
        parser.parse(stream, handler, metadata, new ParseContext());
        return handler.toString();
    }
}


相关文章
|
5月前
|
Java API Apache
使用 Apache PDFBox 操作PDF文件
Apache PDFBox库是一个开源的Java工具,专门用于处理PDF文档。它允许用户创建全新的PDF文件,编辑现有的PDF文档,以及从PDF文件中提取内容。此外,Apache PDFBox还提供了一些命令行实用工具。
404 6
|
8天前
|
Java BI API
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
这篇文章介绍了如何在Spring Boot项目中整合iTextPDF库来导出PDF文件,包括写入大文本和HTML代码,并分析了几种常用的Java PDF导出工具。
94 0
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
|
3月前
|
JavaScript 数据库
文本,在线浏览PDF,一个最简单的文档标准样式,文档预览非常简单的样式,文档管理样式设计,标准,好的设计
文本,在线浏览PDF,一个最简单的文档标准样式,文档预览非常简单的样式,文档管理样式设计,标准,好的设计
|
4月前
|
XML Java 数据格式
Java用xpdf库获取pdf文件的指定范围文本内容
Java用xpdf库获取pdf文件的指定范围文本内容
71 1
|
5月前
|
Python
2024年最全用Python和PIL美化图像:文本覆盖技术实战,Python高级面试题pdf
2024年最全用Python和PIL美化图像:文本覆盖技术实战,Python高级面试题pdf
|
5月前
|
自然语言处理 文字识别 数据可视化
从PDF和图像中提取文本,以供大型语言模型使用
从PDF和图像中提取文本,以供大型语言模型使用
|
5月前
|
Java
Java PDF 相关 1、拷贝多个PDF到一个PDF,并且文件大小变小,文本等信息保留
1、合并多个PDF,并且文件变小,后面添加的文本信息保留
131 0
|
Java Unix Linux
知识分享之Golang——读取pdf中纯文本内容
知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。 知识分享系列目前包含Java、Golang、Linux、Docker等等。
1594 1
知识分享之Golang——读取pdf中纯文本内容
|
Java Apache
通过Apache PDFBox将pdf与图片互相转换以及合并与拆分pdf
通过Apache PDFBox将pdf与图片互相转换以及合并与拆分pdf
874 0
|
Java Apache
通过Apache PDFBox将pdf转换为word
通过Apache PDFBox将pdf转换为word
769 0

推荐镜像

更多