extractous

repository·main·Indexed 23 days ago

https://github.com/yobix-ai/extractous

A high-performance, Rust-based library for extracting text and metadata from unstructured documents such as PDFs, Word files, and HTML. It provides native execution for Python and Rust, supporting buffered reading via StreamReader and OCR capabilities through Tesseract. Version 0.2.1.

Tokens
7.9K
Snippets
24
Records
38
Agent score
80%

What's inside extractous

  1. Quickstart: Extract text from a file in Python

    main

    To extract the text content and metadata from a local file as a string using Python, use the Extractor.extract_file_to_string method. You can also configure the maximum length of the extracted string using set_extract_string_max_length.

    from extractous import Extractor
    
    # Create a new extractor
    extractor = Extractor()
    extractor = extractor.set_extract_string_max_length(1000)
    # if you need an xml
    # extractor = extractor.set_xml_output(True)
    
    # Extract text from a file
    result, metadata = extractor.extract_file_to_string("README.md")
    print(result)
    print(metadata)
  2. Quickstart: Extract text from a file in Rust

    main

    In Rust, the Extractor uses a consuming builder pattern. You can extract file content directly to a string using extract_file_to_string.

    use extractous::Extractor;
    
    fn main() {
        // Create a new extractor. Note it uses a consuming builder pattern
        let mut extractor = Extractor::new().set_extract_string_max_length(1000);
        // if you need an xml
        // extractor = extractor.set_xml_output(true);
    
        // Extract text from a file
        let (text, metadata) = extractor.extract_file_to_string("README.md").unwrap();
        println!("{}", text);
        println!("{:?}", metadata);
    }
  3. Quickstart: Extract file content to a stream in Python

    main

    For large files or when you need to process content incrementally, use extract_file, extract_url, or extract_bytes to obtain a reader (stream) and metadata. This allows for buffered reading of the content.

    from extractous import Extractor
    
    extractor = Extractor()
    # if you need an xml
    # extractor = extractor.set_xml_output(True)
    
    # for file
    reader, metadata = extractor.extract_file("tests/quarkus.pdf")
    # for url
    # reader, metadata = extractor.extract_url("https://www.google.com")
    # for bytearray
    # with open("tests/quarkus.pdf", "rb") as file:
    #     buffer = bytearray(file.read())
    # reader, metadata = extractor.extract_bytes(buffer)
    
    result = ""
    buffer = reader.read(4096)
    while len(buffer) > 0:
        result += buffer.decode("utf-8")
        buffer = reader.read(4096)
    
    print(result)
    print(metadata)
  4. Quickstart: Extract content to a StreamReader in Rust

    main

    To perform buffered reading in Rust, use extract_file, extract_url, or extract_bytes to get a stream that implements the std::io::Read trait. You can wrap this stream in a std::io::BufReader for efficient processing.

    use std::io::{BufReader, Read};
    // use std::fs::File; use for bytes
    use extractous::Extractor;
    
    fn main() {
        // Get the command-line arguments
        let args: Vec<String> = std::env::args().collect();
        let file_path = &args[1];
    
        // Extract the provided file content to a string
        let extractor = Extractor::new();
        // if you need an xml
        // extractor = extractor.set_xml_output(true);
    
        let (stream, metadata) = extractor.extract_file(file_path).unwrap();
        // Extract url
        // let (stream, metadata) = extractor.extract_url("https://www.google.com/").unwrap();
        // Extract bytes
        // let mut file = File::open(file_path)?;
        // let mut buffer = Vec::new();
        // file.read_to_end(&mut buffer)?;
        // let (stream, metadata) = extractor.extract_bytes(&file_bytes);
    
        // Because stream implements std::io::Read trait we can perform buffered reading
        // For example we can use it to create a BufReader
        let mut reader = BufReader::new(stream);
        let mut buffer = Vec::new();
        reader.read_to_end(&mut buffer).unwrap();
    
        println!("{}", String::from_utf8(buffer).unwrap());
        println!("{:?}", metadata);
    }
  5. Build requirements for Extractous

    main

    Extractous uses Apache Tika for non-native formats, compiled as native shared libraries via FFI for performance.

    GraalVM

    • GraalVM is required to build Tika as native libraries.
    • You can set GRAALVM_HOME to use a specific local version.
    • macOS Users: Official GraalVM JDKs may fail with code using java awt. It is recommended to use Bellsoft Liberica NIK (sdk install java 24.1.1.r23-nik).

    Tesseract (for OCR)

    • Debian/Ubuntu: sudo apt install tesseract-ocr and language packs like tesseract-ocr-deu.
    • macOS: brew install tesseract tesseract-lang.
  6. Configure OCR for text extraction

    main

    To extract text from images or scanned documents, you must have Tesseract OCR installed on your system. You can configure the OCR engine using TesseractOcrConfig to specify the language (e.g., set_language("deu")).

    In Python, pass the config to set_ocr_config(). In Rust, use the builder pattern on Extractor with set_ocr_config() and optionally configure PDF-specific OCR strategies via set_pdf_config() with PdfOcrStrategy::OCR_ONLY.

    # Python OCR Example
    from extractous import Extractor, TesseractOcrConfig
    
    extractor = Extractor().set_ocr_config(TesseractOcrConfig().set_language("deu"))
    result, metadata = extractor.extract_file_to_string("../../test_files/documents/eng-ocr.pdf")
    
    print(result)
    print(metadata)
    // Rust OCR Example
    use extractous::Extractor;
    
    fn main() {
      let file_path = "../test_files/documents/deu-ocr.pdf";
    
        let extractor = Extractor::new()
              .set_ocr_config(TesseractOcrConfig::new().set_language("deu"))
              .set_pdf_config(PdfParserConfig::new().set_ocr_strategy(PdfOcrStrategy::OCR_ONLY));
        // extract file with extractor
      let (content, metadata) = extractor.extract_file_to_string(file_path).unwrap();
      println!("{}", content);
      println!("{:?}", metadata);
    }
  7. Extract text with OCR

    main

    To extract text from images or scanned PDFs, you must enable OCR.

    Prerequisites:

    • Ensure Tesseract is installed on your system with the required language packs (e.g., sudo apt install tesseract-ocr tesseract-ocr-deu for German).
    • If you encounter Parse error occurred : Unable to extract PDF content, verify that the OCR language pack is installed.

    Configuration:

    1. Use set_ocr_config with a TesseractOcrConfig instance to specify the language.
    2. Use set_pdf_config with a PdfParserConfig to set the PdfOcrStrategy (e.g., PdfOcrStrategy::OCR_ONLY).
    use extractous::{Extractor, TesseractOcrConfig, PdfParserConfig, PdfOcrStrategy};
    
    let file_path = "../test_files/documents/deu-ocr.pdf";
    
    // Configure extractor for OCR
    let extractor = Extractor::new()
     .set_ocr_config(TesseractOcrConfig::new().set_language("deu"))
     .set_pdf_config(PdfParserConfig::new().set_ocr_strategy(PdfOcrStrategy::OCR_ONLY));
    
    // Extract file content
    let (content, metadata) = extractor.extract_file_to_string(file_path).unwrap();
    println!("{}", content);
  8. Extract PDF content using OCR

    main

    To extract text from scanned PDFs or images within PDFs, configure the extractor with TesseractOcrConfig and set a PdfOcrStrategy.

    Requirement: You must have tesseract installed on your system along with the necessary language packs (e.g., tesseract-ocr-deu for German). If you encounter Parse error occurred : Unable to extract PDF content, verify that the OCR language pack is installed.

    use extractous::Extractor;
    
    fn main() {
      let file_path = "../test_files/documents/deu-ocr.pdf";
    
      let extractor = Extractor::new()
              .set_ocr_config(TesseractOcrConfig::new().set_language("deu"))
              .set_pdf_config(PdfParserConfig::new().set_ocr_strategy(PdfOcrStrategy::OCR_ONLY));
      
      let (content, metadata) = extractor.extract_file_to_string(file_path).unwrap();
      println!("{}", content);
      println!("{:?}", metadata);
    }
  9. Extract file content to a String

    main

    Use extract_file_to_string to quickly get the text content and metadata of a file as a String and a metadata object. This is the simplest way to extract text when you don't need to stream the data.

    use extractous::Extractor;
    
    fn main() {
      let file_path = "path/to/file";
    
      let mut extractor = Extractor::new();
      // Extract text from a file
      let (content, metadata) = extractor.extract_file_to_string(file_path).unwrap();
      println!("{}", content);
      println!("{:?}", metadata);
    }
  10. Extract content to a StreamReader for buffered reading

    main

    If you need to handle large files or want to process data as a stream, use extract_file, extract_url, or extract_bytes. These methods return a StreamReader (which implements std::io::Read) and the metadata. This allows you to use standard Rust tools like BufReader for efficient processing.

    use std::io::{BufReader, Read};
    use extractous::Extractor;
    
    fn main() {
      let file_path = "path/to/file";
    
      let extractor = Extractor::new();
      let (stream, metadata) = extractor.extract_file(file_path).unwrap();
    
      // Because stream implements std::io::Read trait we can perform buffered reading
      let mut reader = BufReader::new(stream);
      let mut buffer = Vec::new();
      reader.read_to_end(&mut buffer).unwrap();
    
      println!("{}", String::from_utf8(buffer).unwrap());
      println!("{:?}", metadata);
    }