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.