skip to Main Content

I have the following Python code to extract text from a locally stored PDF file:

# import libraries
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
import base64

def analyze_layout():
    
    document_intelligence_client = DocumentIntelligenceClient(
        endpoint="https://<resource-name>.cognitiveservices.azure.com/", 
        credential=AzureKeyCredential("EHCGTC....")
    )

    with open("C:/Users/lvg/source/repos/terraform-ai-iac/terraform-ai-iac/data/documents/test.pdf", "rb") as document:
        poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", document)

    result: AnalyzeResult = poller.result()

analyze_layout()

However, when I run this code I get the error:

azure.core.exceptions.ResourceNotFoundError: (404) Resource not found
Code: 404
Message: Resource not found

I am able to go to the resource in Azure and use document intelligence there to extract the text from the PDF that is stored locally.

What could be the reason my code is not working?

2

Answers


  1. The 404 "Resource not found" error typically occurs due to one of these common issues when using the Azure Document Intelligence API:

    Incorrect endpoint URL format – Make sure you’re using the correct format:

    endpoint="https://{your-resource-name}.cognitiveservices.azure.com/"
    Double-check that is replaced with your actual resource name, not left as a placeholder.

    Invalid resource region – The endpoint URL should match the region where your Document Intelligence resource is deployed. Verify this in your Azure portal.

    just fix it by
    Go to your Azure Document Intelligence resource
    Copy the exact endpoint URL from "Keys and Endpoint" section
    Use that URL in your code, ensuring no typos or formatting issues

    Login or Signup to reply.
  2. azure.core.exceptions.ResourceNotFoundError: (404) Resource not found
    Code: 404
    Message: Resource not found.

    "The error might be due to the location, API version, or SDK version being used. If you used correct endpoint and key.

    In my environment, I used SDK with azure-ai-documentintelligence==1.0.0 version and I deployed my Document intelligence studio in east us with standard tier.

    Portal:
    enter image description here

    Now, I used the same code with correct endpoint and key from the portal.

    Code:

    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documentintelligence import DocumentIntelligenceClient
    
    
    def analyze_layout():
        
        document_intelligence_client = DocumentIntelligenceClient(
            endpoint="https://venkatdoc45.cognitiveservices.azure.com/", 
            credential=AzureKeyCredential("xxxxx")
        )
    
        with open(r"C:Downloadstest (1).pdf", "rb") as document:
            poller = document_intelligence_client.begin_analyze_document("prebuilt-layout", document)
    
        AnalyzeResult = poller.result()
        print(AnalyzeResult.content)
    
    analyze_layout()
    

    Output:

    Your Name
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit
    EXPERIENCE
    Company, Location - Job Title MONTH 20XX - PRESENT
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
    Company, Location - Job Title MONTH 20XX - MONTH 20XX
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
    Company, Location - Job Title MONTH 20XX - MONTH 20XX
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
    EDUCATION
    School Name, Location - Degree MONTH 20XX - MONTH 20XX
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore.
    School Name, Location - Degree MONTH 20XX - MONTH 20XX
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam.
    PROJECTS
    

    enter image description here

    Reference:
    Azure AI Document Intelligence client library for Python | Microsoft Learn

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search