I have generated document analysis result using,
with open(pdf_path, "rb") as f:
poller = document_intelligence_client_sections.begin_analyze_document(
"prebuilt-layout", f.read(), content_type="application/pdf",
output_content_format=ContentFormat.MARKDOWN, )
result = poller.result()
type(section_layout)
>> azure.ai.documentintelligence.models._models.AnalyzeResult # Want in this format!
I have saved the result using …as_dict() as follows,
with open("data/section_layouts/result.json", "w") as f:
json.dump(section_layout.as_dict(), f)
Now, as I load the json using,
with open("result.json", "r") as f:
data = json.load(f)
I get the data in dictionary as expected. However, I wanted to have the data in in AnalyzeResult class format. Can anyone please help? Thank you.
More information:
I am using DocumentIntelligenceClient.
from azure.ai.documentintelligence import DocumentIntelligenceClient
document_intelligence_client_sections = DocumentIntelligenceClient(
endpoint=service_endpoint, credential=default_credential)
with open(pdf_path, "rb") as f:
poller = document_intelligence_client_sections.begin_analyze_document(
"prebuilt-layout", f.read(), content_type="application/pdf",
output_content_format=ContentFormat.MARKDOWN,
)
result = poller.result()
So, the type of the object is different,
type(section_layout)
azure.ai.documentintelligence.models._models.AnalyzeResult
And it does not have .to_dict(), and from_dict(), as well.
2
Answers
The solution was rather simple.
Importing AnalyzeResult from azure.ai.documentintelligence
from azure.ai.documentintelligence.models import AnalyzeResult
and the casting the dictionary previously saved with .as_dict()
I wish #Microsof #Azure #DocumentIntelligence team would have added this in their documentation!
You can use the below code to get the data in
AnalyzeResult
class format.Code:
The above code use the
to_dict()
method to save theAnalyzeResult
object as a JSON file and then use thefrom_dict()
method to convert the JSON data back to anAnalyzeResult
object.Output: