Search for Document Intelligence and Content Understanding in the Services tab of your Azure Project page. After the script is run, the extracted text with label and value are in markdown part of the response.
import json
from urllib.parse import urlparse
from azure.ai.contentunderstanding import ContentUnderstandingClient
from azure.ai.contentunderstanding.models import AnalysisInput, AnalysisResult
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import AzureError
from azure.identity import DefaultAzureCredential
def is_absolute_url(value: str) -> bool:
parsed_url = urlparse(value)
return bool(parsed_url.scheme and parsed_url.netloc)
def main() -> None:
# Insert the following configurations.
# 1) AZURE_CONTENT_UNDERSTANDING_ENDPOINT - the endpoint to your Content Understanding resource.
endpoint = "https://redacted-resource.services.ai.azure.com/"
# 2) CONTENT_UNDERSTANDING_KEY - your Content Understanding API key (optional if using DefaultAzureCredential).
key = "redacted-key"
# 3) FILE_URL - you can replace this with your own URL.
file_url = (
"https://raw.githubusercontent.com/Azure-Samples/"
"azure-ai-content-understanding-assets/main/document/invoice.pdf"
)
# ANALYZER_ID - the ID of the analyzer to use.
analyzer_id = "prebuilt-read"
# API_VERSION - the API version to use.
api_version = "2026-06-01-preview"
# Validate Endpoint
if not is_absolute_url(endpoint):
print("[Error] Invalid Endpoint. Please provide a valid 'Endpoint'.")
return
# Validate File URL
if not is_absolute_url(file_url):
print("[Error] Invalid File URL. Please provide a valid URL.")
return
# Set up Content Understanding client.
has_key = (
isinstance(key, str)
and bool(key.strip())
and "{{CONTENT_UNDERSTANDING_KEY}}" not in key
)
credential = AzureKeyCredential(key) if has_key else DefaultAzureCredential()
client = ContentUnderstandingClient(
endpoint=endpoint,
credential=credential,
api_version=api_version,
)
# [START analyze]
print(f"Analyzing with {analyzer_id} analyzer...")
print(f" File URL: {file_url}\n")
try:
poller = client.begin_analyze(
analyzer_id=analyzer_id,
inputs=[AnalysisInput(url=file_url)],
)
result: AnalysisResult = poller.result()
except AzureError as err:
print(f"[Azure Error]: {err}")
return
except Exception as ex:
print(f"[Unexpected Error]: {ex}")
return
# [END analyze]
# [START output_result]
print("=" * 50)
print("Analysis result:")
print("=" * 50 + "\n")
max_display_lines = 50
result_str = json.dumps(result.as_dict(), indent=2)
ret_lines = result_str.splitlines()
if len(ret_lines) > max_display_lines:
print("\n".join(ret_lines[:max_display_lines]))
print(
f"\n {len(ret_lines) - max_display_lines} "
"more lines to be displayed...\n"
)
else:
print(result_str)
# [END output_result]
if __name__ == "__main__":
main()
.venv/bin/python azcontentunderstanding.py
Analyzing with prebuilt-read analyzer...
File URL: https://raw.githubusercontent.com/Azure-Samples/azure-ai-content-understanding-assets/main/document/invoice.pdf
==================================================
Analysis result:
==================================================
{
"analyzerId": "prebuilt-read",
"apiVersion": "2026-06-01-preview",
"createdAt": "2026-11-15T12:54:05Z",
"stringEncoding": "codePoint",
"warnings": [],
"contents": [
{
"path": "input1",
"markdown": "CONTOSO LTD.\n\nINVOICE\n\nContoso Headquarters\n123 456th St\nNew York, NY, 10001\n\nINVOICE: INV-100\n\nINVOICE DATE: 11/15/2019\n\nDUE DATE: 12/15/2019\n\nCUSTOMER NAME: MICROSOFT CORPORATION\n\nSERVICE PERIOD: 10/14/2019 - 11/14/2019\n\nCUSTOMER ID: CID-12345\n\nMicrosoft Corp\n123 Other St,\nRedmond WA, 98052\n\nBILL TO:\nMicrosoft Finance\n123 Bill St,\nRedmond WA, 98052\n\nSHIP TO:\nMicrosoft Delivery\n123 Ship St,\nRedmond WA, 98052\n\nSERVICE ADDRESS:\nMicrosoft Services\n123 Service St,\nRedmond WA, 98052\n\nSALESPERSON\n\nP.O. NUMBER\n\nREQUISITIONER\n\nSHIPPED VIA\n\nF.O.B. POINT\n\nTERMS\n\nPO-3333\n\nDATE\n\nITEM CODE\n\nDESCRIPTION\n\nQTY\n\nUM\n\nPRICE\n\nTAX\n\nAMOUNT\n\n3/4/2021\n\nA123\n\nConsulting Services\n\n2\n\nhours\n\n$30.00\n\n$6.00\n\n$60.00\n\n3/5/2021\n\nB456\n\nDocument Fee\n\n3\n\n$10.00\n\n$3.00\n\n$30.00\n\n3/6/2021\n\nC789\n\nPrinting Fee\n\n10\n\npages\n\n$1.00\n\n$1.00\n\n$10.00\n\nSUBTOTAL\n\n$100.00\n\nSALES TAX\n\n$10.00\n\nTOTAL\n\n$110.00\n\nPREVIOUS UNPAID BALANCE\n\n$500.00\n\nAMOUNT DUE\n\n$610.00\n\nTHANK YOU FOR YOUR BUSINESS!\n\nREMIT TO:\nContoso Billing\n123 Remit St\nNew York, NY, 10001\n",
"kind": "document",
"startPageNumber": 1,
"endPageNumber": 1,
"unit": "inch",
"pages": [
{
"pageNumber": 1,
"angle": 0,
"width": 8.5,
"height": 11,
"spans": [
{
"offset": 0,
"length": 1015
}
],
"words": [
{
"content": "CONTOSO",
"span": {
"offset": 0,
"length": 7
},
"confidence": 0.997,
"source": "D(1,0.5753,0.6583,1.7312,0.6573,1.7312,0.8908,0.5753,0.8897)"
},
{
"content": "LTD",
"span": {
"offset": 8,
"length": 3
},
"confidence": 0.998,
"source": "D(1,1.8138,0.6573,2.2423,0.6577,2.2423,0.8881,1.8138,0.8905)"
},
{
"content": ".",
"span": {
"offset": 11,
"length": 1
2905 more lines to be displayed...