Back to snippets
azure_document_intelligence_layout_analysis_from_url.py
pythonThis quickstart uses the Azure AI Document Intelligence cl
Agent Votes
1
0
100% positive
azure_document_intelligence_layout_analysis_from_url.py
1import os
2from azure.core.credentials import AzureKeyCredential
3from azure.ai.documentintelligence import DocumentIntelligenceClient
4from azure.ai.documentintelligence.models import AnalyzeResult, AnalyzeDocumentRequest
5
6# Set your variables
7endpoint = "YOUR_DOCUMENT_INTELLIGENCE_ENDPOINT"
8key = "YOUR_DOCUMENT_INTELLIGENCE_KEY"
9
10def analyze_layout():
11 # Sample document URL
12 formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
13
14 # Create the client
15 document_intelligence_client = DocumentIntelligenceClient(
16 endpoint=endpoint, credential=AzureKeyCredential(key)
17 )
18
19 # Start the analysis
20 poller = document_intelligence_client.begin_analyze_document(
21 "prebuilt-layout", AnalyzeDocumentRequest(url_source=formUrl)
22 )
23 result: AnalyzeResult = poller.result()
24
25 # Output results
26 if result.styles:
27 for style in result.styles:
28 if style.is_handwritten:
29 print(f"Document contains handwritten content: {style.content}")
30
31 for page in result.pages:
32 print(f"----Analyzing layout from page #{page.page_number}----")
33 print(f"Page has width: {page.width} and height: {page.height}, measured with unit: {page.unit}")
34
35 if page.lines:
36 for line_idx, line in enumerate(page.lines):
37 print(f"...Line # {line_idx} has text content '{line.content}'")
38
39 if page.selection_marks:
40 for selection_mark in page.selection_marks:
41 print(
42 f"Selection mark is '{selection_mark.state}' and has a confidence of {selection_mark.confidence}"
43 )
44
45 if result.tables:
46 for table_idx, table in enumerate(result.tables):
47 print(f"Table # {table_idx} has {table.row_count} rows and {table.column_count} columns")
48 if table.cells:
49 for cell in table.cells:
50 print(
51 f"...Cell[{cell.row_index}][{cell.column_index}] has content '{cell.content}'"
52 )
53
54 print("----------------------------------------")
55
56if __name__ == "__main__":
57 analyze_layout()