Get Searchable PDF from Azure Document Intelligence Service

Fred 95 Reputation points
2025-09-25T23:42:51.96+00:00

I'm trying to use python to convert a scanned PDF into a searchable PDF. I see here that it is supported but I am looking for a code example or sample on how to do this. Where I am struggling is the request seems to require a request_id but I am unclear on how to get that.

I have tried using the document intelligence service. The below code is not working

from loguru import logger
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.models import (
    AnalyzeDocumentRequest,
)
from azure.ai.documentintelligence import DocumentIntelligenceClient
import os
def extract_pdf(
    file: str,
    # tables: DBTables,
    use_cache: bool = True,
    text_model: str = "prebuilt-read",
) -> str:
    """creates markdown formatted text"""
    logger.info("=======Starting Text Extraction=======")
    filename, file_extension = os.path.splitext(file)
    out = filename + ".ocr.pdf"
    if use_cache:
        logger.debug("Checking cache")
        if os.path.isfile(out):
            logger.debug("Results found in cache, done")
            with open(out, "r") as f:
                return f.read()
        logger.debug("Results not in cache, extracting")
    logger.debug("running poller")
    credential = AzureKeyCredential("MYKEY")
    analysis_client = DocumentIntelligenceClient(
        "MYENDPOINT", credential
    )
    with open(file, "rb") as f:
        poller = analysis_client.begin_analyze_document(
            text_model,
            AnalyzeDocumentRequest(bytes_source=f.read()),
            output=["pdf"],
        )
    result = poller.result()
    print(result)
    with open(out, "wb") as f:
        f.write(result)
    return out

Any guidance or a working example would be appreciated

Azure Document Intelligence in Foundry Tools

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.