document translation python error

Dorin Ben Haim 0 Reputation points
2024-11-14T12:12:26.1666667+00:00

Hi, i try use sample code from your docomentation

the code:

import os

from azure.core.credentials import AzureKeyCredential

from azure.ai.translation.document import SingleDocumentTranslationClient

from azure.ai.translation.document.models import DocumentTranslateContent

def sample_single_document_translation():

# create variables for your resource api key, document translation endpoint, and target language

key = "
Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
429 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 14,616 Reputation points
    2024-11-14T13:13:51.6466667+00:00

    Hello Dorin Ben Haim,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you are having document translation issue with the method you’re trying to call on the SingleDocumentTranslationClient object.

    The error message indicates that the document_translate method does not exist and this might be due to a typo or an incorrect method name. The method you should use is begin_translation, which is part of the DocumentTranslationClient class, not SingleDocumentTranslationClient.

    This is an updated version of your code:

    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document import DocumentTranslationClient
    from azure.ai.translation.document.models import DocumentTranslationInput, TranslationTarget
    def sample_single_document_translation():
        # create variables for your resource api key, document translation endpoint, and target language
        key = "<your-api-key>"
        endpoint = "<your-document-translation-endpoint>"
        target_language = "<target-language-code>"
        # initialize a new instance of the DocumentTranslationClient object to interact with the synchronous Document Translation feature
        client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
        # absolute path to your document
        file_path = "C:/your-file-path/document-translation-sample.docx"
        file_name = os.path.basename(file_path)
        print(f"File for translation: {file_name}")
        # create the translation input
        translation_input = DocumentTranslationInput(
            source_url=file_path,
            targets=[
                TranslationTarget(
                    target_url=f"C:/your-file-path/translated-{file_name}",
                    language=target_language
                )
            ]
        )
        # start the translation
        poller = client.begin_translation(inputs=[translation_input])
        result = poller.result()
        for document in result:
            print(f"Document status: {document.status}")
            print(f"Translated document URL: {document.translated_document_url}")
    if __name__ == "__main__":
        sample_single_document_translation()
    

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    1 person found this answer helpful.
    0 comments No comments

Your answer

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