Output adapters

Output adapters are used to output generated HTML or plain text to various output objects. The output format (HTML or plain text) is specified by the OutputFormat value passed to the constructor of an adapter. The following output adapters are currently supported:

  • FileOutputAdapter is an output adapter that is used to output text to a file. The path to a file is specified in the constructor of the adapter.
  • StreamOutputAdapter is an output adapter that is used to output text to a stream. The stream is specified in the constructor of the adapter; in Python, pass a binary file object opened for writing, for example, the one returned by open(path, "wb").
  • StringOutputAdapter is an output adapter that is used to output text to a string. The resulting string can be accessed through the get_result method of the adapter class.
  • StructureOutputAdapter is an output adapter that is used to output the text of each field of the document separately. The resulting list of fields can be accessed through the get_result method of the adapter class.

The example below demonstrates how to use adapters of different types.

import os

from groupdocs.search import Index, IndexSettings
from groupdocs.search.common import FileOutputAdapter, StreamOutputAdapter, StringOutputAdapter, StructureOutputAdapter
from groupdocs.search.options import Compression, OutputFormat, TextStorageSettings

# Creating an index settings instance
settings = IndexSettings()
settings.text_storage_settings = TextStorageSettings(Compression.HIGH)  # Enabling storage of extracted text in the index

# Creating an index in the specified folder
index = Index("./index", settings)

# Indexing documents from the specified folder
index.add("./documents")

# Getting list of indexed documents
documents = index.get_indexed_documents()

# Getting a document text
if len(documents) > 0:
    document = documents[0]
    os.makedirs("./output", exist_ok=True)

    # Output to a file
    file_output_adapter = FileOutputAdapter(OutputFormat.HTML, "./output/Text.html")
    index.get_document_text(document, file_output_adapter)

    # Output to a stream
    with open("./output/Stream.html", "wb") as stream:
        stream_output_adapter = StreamOutputAdapter(OutputFormat.HTML, stream)
        index.get_document_text(document, stream_output_adapter)

    # Output to a string
    string_output_adapter = StringOutputAdapter(OutputFormat.HTML)
    index.get_document_text(document, string_output_adapter)
    html_text = string_output_adapter.get_result()
    print(html_text)

    # Output to a structure
    structure_output_adapter = StructureOutputAdapter(OutputFormat.PLAIN_TEXT)
    index.get_document_text(document, structure_output_adapter)
    fields = structure_output_adapter.get_result()
    print(document.to_string())
    for field in fields:
        print("\t" + field.name)

The following sample file is used in this example: documents/einstein-biography.txt

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>documents\einstein-biography.txt</title>
</head>
<body>
<p>documents\einstein-biography.txt</p>
<p></p>
<h3>filename</h3>
[TRUNCATED]

Download full output

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with the full-featured library, we provide simple but powerful free apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.