Extract to Stream

How to extract the content from a file to a stream.

Efficiently processes large files by streaming them in chunks, enabling handling of documents of any size without full memory loading.

from extractous import Extractor

def extract_content_stream():
    extractor = Extractor()
    reader = extractor.extract_file("path/to/document.pdf")
    
    # Process in chunks
    chunk_size = 4096
    result = ""
    while True:
        buffer = reader.read(chunk_size)
        if not buffer:
            break
        result += buffer.decode("utf-8")
    
    return result

# Usage with error handling
try:
    content = extract_content_stream()
    print(content)
except Exception as e:
    print(f"Error extracting content: {e}")