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.
importosfromgroupdocs.searchimportIndex,IndexSettingsfromgroupdocs.search.commonimportFileOutputAdapter,StreamOutputAdapter,StringOutputAdapter,StructureOutputAdapterfromgroupdocs.search.optionsimportCompression,OutputFormat,TextStorageSettings# Creating an index settings instancesettings=IndexSettings()settings.text_storage_settings=TextStorageSettings(Compression.HIGH)# Enabling storage of extracted text in the index# Creating an index in the specified folderindex=Index("./index",settings)# Indexing documents from the specified folderindex.add("./documents")# Getting list of indexed documentsdocuments=index.get_indexed_documents()# Getting a document textiflen(documents)>0:document=documents[0]os.makedirs("./output",exist_ok=True)# Output to a filefile_output_adapter=FileOutputAdapter(OutputFormat.HTML,"./output/Text.html")index.get_document_text(document,file_output_adapter)# Output to a streamwithopen("./output/Stream.html","wb")asstream:stream_output_adapter=StreamOutputAdapter(OutputFormat.HTML,stream)index.get_document_text(document,stream_output_adapter)# Output to a stringstring_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 structurestructure_output_adapter=StructureOutputAdapter(OutputFormat.PLAIN_TEXT)index.get_document_text(document,structure_output_adapter)fields=structure_output_adapter.get_result()print(document.to_string())forfieldinfields:print("\t"+field.name)