Hi Martin Møldrup,
Greetings & Welcome to the Microsoft Q&A forum! Thank you for sharing your query.
Based on your request, here is the updated code to analyze documents using Azure Document Intelligence and extract figures.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeOutputOption, AnalyzeResult
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
with open("path_to_sample_documents", "rb") as f:
poller = document_intelligence_client.begin_analyze_batch_documents(
model_id="prebuilt-layout",
body=f,
output=[AnalyzeOutputOption.FIGURES],
)
# Retrieve the continuation token
continuation_token = poller.continuation_token()
# Get the batch result using the continuation token
batch_result = document_intelligence_client.get_analyze_batch_result(continuation_token)
# Iterate through the results and download figures
for result in batch_result.results:
if result.figures:
for figure in result.figures:
if figure.id:
response = document_intelligence_client.get_analyze_result_figure(
model_id=result.model_id, result_id=result.id, figure_id=figure.id
)
with open(f"{figure.id}.png", "wb") as writer:
writer.write(response.read())
else:
print("No figures found.")
print("Figures downloaded successfully.")
Please try the above code and let us know if you are facing any issues.
I hope this information helps.