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.
You can use the below code to get the data in AnalyzeResult class format.
Code:
import json
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import AnalyzeResult
endpoint = "https://xxxx.cognitiveservices.azure.com/"
api_key = "xxxxx"
document_intelligence_client_sections = DocumentAnalysisClient(endpoint, AzureKeyCredential(api_key))
pdf_path = r"C:\Users\xxx\demo.pdf"
with open(pdf_path, "rb") as f:
poller = document_intelligence_client_sections.begin_analyze_document(
model_id="prebuilt-layout", document=f.read(),
)
section_layout = poller.result()
with open("result.json", "w") as f:
json.dump(section_layout.to_dict(), f) # Use to_dict() to save as JSON
with open("result.json", "r") as f:
data = json.load(f)
# Convert the dictionary back to an AnalyzeResult object
analyze_result = AnalyzeResult.from_dict(data)
# Now you can work with the AnalyzeResult object
print(type(analyze_result))
The above code use the to_dict() method to save the AnalyzeResult object as a JSON file and then use the from_dict() method to convert the JSON data back to an AnalyzeResult object.
Output:
<class 'azure.ai.formrecognizer._models.AnalyzeResult'>
