admin管理员组

文章数量:1124799

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/", 
        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?

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?

Share Improve this question asked 2 days ago Luuk van GasterenLuuk van Gasteren 133 bronze badges 4
  • Make sure Azure Document Intelligence api key is in correct? – Venkatesan Commented 2 days ago
  • I get the API key from the Keys and Endpoint section in the Document Intelligence resource in the portal – Luuk van Gasteren Commented 2 days ago
  • Can you share more? which location it is provisioned? and also pricing tier? and is there is network is enabled for all networks? – Venkatesan Commented yesterday
  • Check the below answer. – Venkatesan Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

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:

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:\Downloads\test (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

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

本文标签: pythonAzure Document Intelligence beginanalyzedocument method errorStack Overflow