Leveraging Azure Document Intelligence Layout API with markdown output will provide all content in a single response by default. You can split the markdown content per page, by looking at the page information (span offset and length) provided in the response.
See below code example (Python) which can be used to split the markdown content:
# Split markdown content per page
content_per_page = []
page_number = 1
for page in result.pages:
content = result.content[page.spans[0]['offset']: page.spans[0]['offset'] + page.spans[0]['length']]
content_per_page.append({"page_number": page_number, "content": content})
page_number+=1
using Azure Document Intelligence to extract content from documents and would like to separate the content page by page:
- Followed this Azure AI Document Intelligence client with python
for page in result.pages:
# Create a string to hold the Markdown content for this page
markdown_content = ""
# Iterate through each element on the page
for element in page.lines:
# Append each line to the markdown_content string
markdown_content += f"{element.content}\n"
# Append the markdown content to the markdown_pages list
markdown_pages.append(markdown_content)
The below Python code extract content from documents and separates the content page by page
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
# Set up the client
endpoint ="https://your-service-endpoint.cognitiveservices.azure.com/"
api_key = "your-api-key"
client = DocumentAnalysisClient(endpoint, AzureKeyCredential(api_key))
# Analyze the document
document_url = "https://sampath1233sa23344545.blob.core.windows.net/sampathpujari/sample-layout_merged%20(1).pdf?sp=r&st=2024-04-17T11:50:23Z&se=2024-04-17T19:50:23Z&sv=2022-11-02&sr=b&sig=r6USzR4zcaECjsNw9HAbJ%2BvzRwrk3InrBmaXklxbs5c%3D"
# Begin document analysis
analysis_poller = client.begin_analyze_document_from_url("prebuilt-document", document_url)
result = analysis_poller.result() # Wait for the analysis to complete and get the result
# Initialize a list to hold the markdown content for each page
markdown_pages = []
# Iterate through each page
for page in result.pages:
# Create a string to hold the Markdown content for this page
markdown_content = ""
# Iterate through each element on the page
for element in page.lines:
# Append each line to the markdown_content string
markdown_content += f"{element.content}\n"
# Append the markdown content to the markdown_pages list
markdown_pages.append(markdown_content)
# The markdown_pages list now contains the content of each page in Markdown format
for i, content in enumerate(markdown_pages):
print(f"Page {i + 1}:\n{content}\n")
Output:


- Refer to this for Analyze complex documents with Azure Document Intelligence Markdown Output
It is possible to override the API version while initialising the DocumentIntelligenceClient to use a different version than the one that the Python client library was developed for.
However, please note that Microsoft warns that:
overriding this default value (for api_version) may result in unsupported behavior
The below code sample shows using the api_version to override the API version from 2024-02-29-preview to 2024-07-31-preview while using the 1.0.0b2 version of the azure-ai-documentintelligence client library.
# pip install azure-ai-documentintelligence==1.0.0b2
from azure.ai.documentintelligence import DocumentIntelligenceClient
document_intelligence_client = DocumentIntelligenceClient(
endpoint = <<Your endpoint>>,
credential = <<Your credential>>,
api_version = "2024-07-31-preview"
)
Also, note that Microsoft is yet to release an official client library that targets the 2024-07-31-preview version of the API. However, once they do release the official client library supporting the 2024-07-31-preview version of the API, we should update to that version of the client library and remove the api_version parameter while initialising the document intelligence client.
According to this doc the Document Intelligence 2024-02-29-preview REST API is now available. This preview API introduces new and updated capabilities:
Public preview version 2024-02-29-preview is currently available only in the following Azure regions:
- East US
- West US2
- West Europe
Install the library using pip
pip install azure-ai-documentintelligence==1.0.0b2
pip install azure-ai-formrecognizer
- Get started with Azure AI Document Intelligence latest preview version (2024-02-29-preview) reference doc
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.exceptions import HttpResponseError
# Replace these values with your actual endpoint and key
endpoint = "<your-endpoint>"
key = "<your-key>"
# Sample document URL
formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
try:
# Create Document Analysis client
document_analysis_client = DocumentAnalysisClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
# Analyze the document layout
poller = document_analysis_client.begin_analyze_document_from_url('prebuilt-layout', formUrl, locale='de')
results = poller.result()
# Process the analysis results
print("Document analysis results:")
print(results)
except HttpResponseError as e:
print("HTTP Response Error:", e.response.status_code, e.message)
except Exception as ex:
print("An unexpected error occurred:", ex)
Output:
