azure-ai-translation-document-py
microsoft/skills
Traduisez des documents Word, PDF, Excel, PowerPoint et autres à grande échelle à l'aide du SDK Azure AI Document Translation, tout en conservant leur mise en page.
...Développer toutSDK Azure AI pour la traduction de documents en Python
Bibliothèque cliente pour le service de traduction de documents Azure AI Translator, permettant la traduction par lots de documents tout en conservant leur format.
Installation
pip install azure-ai-translation-document
Variables d'environnement
AZURE_DOCUMENT_TRANSLATION_ENDPOINT=https://.cognitiveservices.azure.com # Requis pour toutes les méthodes d'authentification
# Stockage des documents source et cible
AZURE_SOURCE_CONTAINER_URL=https://.blob.core.windows.net/? # Requis pour toutes les méthodes d’authentification
AZURE_TARGET_CONTAINER_URL=https://.blob.core.windows.net/? # Requis pour toutes les méthodes d’authentification
AZURE_TOKEN_CREDENTIALS=prod # Obligatoire uniquement si DefaultAzureCredential est utilisé en production
AZURE_DOCUMENT_TRANSLATION_KEY= # Obligatoire uniquement pour le chemin d'authentification par clé API hérité ci-dessous
Authentification et cycle de vie
🔑 Deux règles s’appliquent à tous les exemples de code ci-dessous :
- Privilégiez
DefaultAzureCredential. Elle fonctionne en local (CLI Azure / VS Code / CLI développeur) et dans Azure (identité gérée, identité de charge de travail) sans modification du code. Évitez les chaînes de connexion, les identifiants de compte et les clés API : ils contournent l’audit et la rotation Entra.
- Développement local :
DefaultAzureCredentialfonctionne tel quel.- En production : définissez
AZURE_TOKEN_CREDENTIALS=prod(ouAZURE_TOKEN_CREDENTIALS=) pour limiter la chaîne d’identifiants aux identifiants sécurisés pour la production.- Enveloppez chaque client dans un gestionnaire de contexte afin que les transports HTTP, les sockets et les caches de jetons soient libérés de manière déterministe :
- Synchrone :
avec `(...)` comme client : - Asynchrone :
async avecet(...) comme client : async avec DefaultAzureCredential() comme identifiant :(deazure.identity.aio)Les extraits de code peuvent simplifier cette configuration, mais le code de production doit toujours respecter ces deux règles.
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.ai.translation.document import DocumentTranslationClient
# Développement local : DefaultAzureCredential. Production : définissez AZURE_TOKEN_CREDENTIALS=prod ou AZURE_TOKEN_CREDENTIALS=
credential = DefaultAzureCredential(require_envvar=True)
# Ou utilisez directement des informations d'identification spécifiques en production :
# Voir https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
with DocumentTranslationClient(
endpoint=os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"],
credential=credential,
) as client:
statuses = list(client.list_translation_statuses())
Ancien système : clé API (déploiements existants avec clé)
Le nouveau code doit utiliser DefaultAzureCredential ci-dessus. N’utilisez AzureKeyCredential que si vous disposez d’un déploiement existant utilisant une clé qui n’a pas encore été migré vers Entra ID — par exemple, les environnements réglementés qui sont encore en cours de déploiement d’Entra.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient, SingleDocumentTranslationClient
with DocumentTranslationClient(
endpoint=os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"],
credential=AzureKeyCredential(os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]),
) as client:
statuses = list(client.list_translation_statuses())
# SingleDocumentTranslationClient accepte les mêmes informations d’identification basées sur une clé.
Traduction de document de base
from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget
source_url = os.environ["AZURE_SOURCE_CONTAINER_URL"]
target_url = os.environ["AZURE_TARGET_CONTAINER_URL"]
# Lancer la tâche de traduction
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(
target_url=target_url,
language="es" # Traduire en espagnol
)
]
)
]
)
# Attendre la fin du traitement
result = poller.result()
print(f"Statut : {poller.status()}")
print(f"Documents traduits : {poller.details.documents_succeeded_count}")
print(f"Documents ayant échoué : {poller.details.documents_failed_count}")
Plusieurs langues cibles
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(target_url=target_url_es, language="es"),
TranslationTarget(target_url=target_url_fr, language="fr"),
TranslationTarget(target_url=target_url_de, language="de")
]
)
]
)
Traduire un document unique
from azure.ai.translation.document import SingleDocumentTranslationClient
from azure.identity import DefaultAzureCredential
with open("document.docx", "rb") as f:
document_content = f.read()
with SingleDocumentTranslationClient(endpoint, DefaultAzureCredential()) as single_client:
result = single_client.translate(
body=document_content,
target_language="es",
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
# Enregistrer le document traduit
with open("document_es.docx", "wb") as f:
f.write(result)
Vérifier l'état de la traduction
# Récupérer toutes les opérations de traduction
operations = client.list_translation_statuses()
for op in operations:
print(f"ID de l'opération : {op.id}")
print(f"Statut : {op.status}")
print(f"Créé le : {op.created_on}")
print(f"Nombre total de documents : {op.documents_total_count}")
print(f"Réussies : {op.documents_succeeded_count}")
print(f"Échouées : {op.documents_failed_count}")
Liste des statuts des documents
# Récupérer le statut de chaque document d’une tâche
operation_id = poller.id
document_statuses = client.list_document_statuses(operation_id)
for doc in document_statuses:
print(f"Document : {doc.source_document_url}")
print(f" Statut : {doc.status}")
print(f" Traduit vers : {doc.translated_to}")
if doc.error:
print(f" Erreur : {doc.error.message}")
Annuler la traduction
# Annuler une traduction en cours
client.cancel_translation(operation_id)
Utilisation du glossaire
from azure.ai.translation.document import TranslationGlossary
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(
target_url=target_url,
language="es",
glossaries=[
TranslationGlossary(
glossary_url="https://.blob.core.windows.net/glossary/terms.csv?",
file_format="csv"
)
]
)
]
)
]
)
Formats de documents pris en charge
# Récupérer les formats pris en charge
formats = client.get_supported_document_formats()
for fmt in formats:
print(f"Format : {fmt.format}")
print(f" Extensions : {fmt.file_extensions}")
print(f" Types de contenu : {fmt.content_types}")
Langues prises en charge
# Récupérer les langues prises en charge
languages = client.get_supported_languages()
for lang in languages:
print(f"Langue : {lang.name} ({lang.code})")
Client asynchrone
from azure.ai.translation.document.aio import DocumentTranslationClient
from azure.identity.aio import DefaultAzureCredential
async def translate_documents():
async with DefaultAzureCredential() as credential:
async with DocumentTranslationClient(
endpoint=endpoint,
credential=credential,
) as client:
poller = await client.begin_translation(inputs=[...])
result = await poller.result()
Formats pris en charge
| Catégorie | Formats |
|---|---|
| Documents | DOCX, PDF, PPTX, XLSX, HTML, TXT, RTF |
| Structurés | CSV, TSV, JSON, XML |
| Localisation | XLIFF, XLF, MHTML |
Exigences de stockage
- Les conteneurs source et cible doivent être hébergés sur Azure Blob Storage
- Utilisez des jetons SAS dotés des autorisations appropriées :
- Source : lecture, liste
- Cible : Écriture, Liste
Bonnes pratiques
- Optez pour le mode synchrone OU asynchrone et restez cohérent. Ne mélangez pas les clients synchrones
azure.xxxavec les clients asynchronesazure.xxx.aiodans un même chemin d’appel. Choisissez un seul mode par module. - Utilisez toujours des gestionnaires de contexte pour les clients et les informations d’identification asynchrones. Enveloppez chaque client
avec `Client(...)` en tant que client :(synchrone) ouasynchrone avec `Client(...)` en tant que client :(asynchrone). Pour les identifiants asynchronesDefaultAzureCredentialdeazure.identity.aio, utilisez également« async » avec « credential: »afin que les jetons et les transports soient nettoyés. - Utilisez des jetons SAS avec le minimum d’autorisations requises
- Surveillez les opérations de longue durée à l’aide de `
poller.status()` - Gérez les erreurs au niveau des documents en parcourant les statuts des documents
- Utilisez des glossaires pour la terminologie spécifique à un domaine
- Prévoyezdes conteneurs cibles distincts pour chaque langue
- Utilisez le client asynchrone pour plusieurs tâches simultanées
- Vérifiez les formats pris en charge avant de soumettre des documents
---
name: azure-ai-translation-document-py
description: Translate Word, PDF, Excel, PowerPoint, and other documents at scale using Azure AI Document Translation SDK with format preservation.
license: MIT
---
# Azure AI Document Translation SDK for Python
Client library for Azure AI Translator document translation service for batch document translation with format preservation.
## Installation
```bash
pip install azure-ai-translation-document
```
## Environment Variables
```bash
AZURE_DOCUMENT_TRANSLATION_ENDPOINT=https://<resource>.cognitiveservices.azure.com # Required for all auth methods
# Storage for source and target documents
AZURE_SOURCE_CONTAINER_URL=https://<storage>.blob.core.windows.net/<container>?<sas> # Required for all auth methods
AZURE_TARGET_CONTAINER_URL=https://<storage>.blob.core.windows.net/<container>?<sas> # Required for all auth methods
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
AZURE_DOCUMENT_TRANSLATION_KEY=<your-api-key> # Only required for the legacy API-key auth path below
```
## Authentication & Lifecycle
> **🔑 Two rules apply to every code sample below:**
>
> 1. **Prefer `DefaultAzureCredential`.** It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
> - Local dev: `DefaultAzureCredential` works as-is.
> - Production: set `AZURE_TOKEN_CREDENTIALS=prod` (or `AZURE_TOKEN_CREDENTIALS=<specific_credential>`) to constrain the credential chain to production-safe credentials.
> 2. **Wrap every client in a context manager** so HTTP transports, sockets, and token caches are released deterministically:
> - Sync: `with <Client>(...) as client:`
> - Async: `async with <Client>(...) as client:` **and** `async with DefaultAzureCredential() as credential:` (from `azure.identity.aio`)
>
> Snippets may abbreviate this setup, but production code should always follow both rules.
```python
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.ai.translation.document import DocumentTranslationClient
# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Or use a specific credential directly in production:
# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
with DocumentTranslationClient(
endpoint=os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"],
credential=credential,
) as client:
statuses = list(client.list_translation_statuses())
```
### Legacy: API Key (existing keyed deployments)
New code should use `DefaultAzureCredential` above. Use `AzureKeyCredential` only if you have an existing keyed deployment that hasn't been migrated to Entra ID yet — for example, regulated environments still completing their Entra rollout.
```python
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient, SingleDocumentTranslationClient
with DocumentTranslationClient(
endpoint=os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"],
credential=AzureKeyCredential(os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]),
) as client:
statuses = list(client.list_translation_statuses())
# SingleDocumentTranslationClient accepts the same key-based credential.
```
## Basic Document Translation
```python
from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget
source_url = os.environ["AZURE_SOURCE_CONTAINER_URL"]
target_url = os.environ["AZURE_TARGET_CONTAINER_URL"]
# Start translation job
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(
target_url=target_url,
language="es" # Translate to Spanish
)
]
)
]
)
# Wait for completion
result = poller.result()
print(f"Status: {poller.status()}")
print(f"Documents translated: {poller.details.documents_succeeded_count}")
print(f"Documents failed: {poller.details.documents_failed_count}")
```
## Multiple Target Languages
```python
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(target_url=target_url_es, language="es"),
TranslationTarget(target_url=target_url_fr, language="fr"),
TranslationTarget(target_url=target_url_de, language="de")
]
)
]
)
```
## Translate Single Document
```python
from azure.ai.translation.document import SingleDocumentTranslationClient
from azure.identity import DefaultAzureCredential
with open("document.docx", "rb") as f:
document_content = f.read()
with SingleDocumentTranslationClient(endpoint, DefaultAzureCredential()) as single_client:
result = single_client.translate(
body=document_content,
target_language="es",
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
# Save translated document
with open("document_es.docx", "wb") as f:
f.write(result)
```
## Check Translation Status
```python
# Get all translation operations
operations = client.list_translation_statuses()
for op in operations:
print(f"Operation ID: {op.id}")
print(f"Status: {op.status}")
print(f"Created: {op.created_on}")
print(f"Total documents: {op.documents_total_count}")
print(f"Succeeded: {op.documents_succeeded_count}")
print(f"Failed: {op.documents_failed_count}")
```
## List Document Statuses
```python
# Get status of individual documents in a job
operation_id = poller.id
document_statuses = client.list_document_statuses(operation_id)
for doc in document_statuses:
print(f"Document: {doc.source_document_url}")
print(f" Status: {doc.status}")
print(f" Translated to: {doc.translated_to}")
if doc.error:
print(f" Error: {doc.error.message}")
```
## Cancel Translation
```python
# Cancel a running translation
client.cancel_translation(operation_id)
```
## Using Glossary
```python
from azure.ai.translation.document import TranslationGlossary
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(
target_url=target_url,
language="es",
glossaries=[
TranslationGlossary(
glossary_url="https://<storage>.blob.core.windows.net/glossary/terms.csv?<sas>",
file_format="csv"
)
]
)
]
)
]
)
```
## Supported Document Formats
```python
# Get supported formats
formats = client.get_supported_document_formats()
for fmt in formats:
print(f"Format: {fmt.format}")
print(f" Extensions: {fmt.file_extensions}")
print(f" Content types: {fmt.content_types}")
```
## Supported Languages
```python
# Get supported languages
languages = client.get_supported_languages()
for lang in languages:
print(f"Language: {lang.name} ({lang.code})")
```
## Async Client
```python
from azure.ai.translation.document.aio import DocumentTranslationClient
from azure.identity.aio import DefaultAzureCredential
async def translate_documents():
async with DefaultAzureCredential() as credential:
async with DocumentTranslationClient(
endpoint=endpoint,
credential=credential,
) as client:
poller = await client.begin_translation(inputs=[...])
result = await poller.result()
```
## Supported Formats
| Category | Formats |
|----------|---------|
| Documents | DOCX, PDF, PPTX, XLSX, HTML, TXT, RTF |
| Structured | CSV, TSV, JSON, XML |
| Localization | XLIFF, XLF, MHTML |
## Storage Requirements
- Source and target containers must be Azure Blob Storage
- Use SAS tokens with appropriate permissions:
- Source: Read, List
- Target: Write, List
## Best Practices
1. **Pick sync OR async and stay consistent.** Do not mix `azure.xxx` sync clients with `azure.xxx.aio` async clients in the same call path. Choose one mode per module.
2. **Always use context managers for clients and async credentials.** Wrap every client in `with Client(...) as client:` (sync) or `async with Client(...) as client:` (async). For async `DefaultAzureCredential` from `azure.identity.aio`, also use `async with credential:` so tokens and transports are cleaned up.
3. **Use SAS tokens** with minimal required permissions
4. **Monitor long-running operations** with `poller.status()`
5. **Handle document-level errors** by iterating document statuses
6. **Use glossaries** for domain-specific terminology
7. **Separate target containers** for each language
8. **Use async client** for multiple concurrent jobs
9. **Check supported formats** before submitting documents
Tous les fichiers
0 fichiersInstaller azure-ai-translation-document-py
Téléchargez et décompressez les fichiers de compétences dans votre répertoire .claude/skills/.
Télécharger le ZIPClonez le dépôt et copiez les fichiers de compétence dans votre projet.
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/azure-ai-translation-document-py # Copy SKILL.md to your .claude/skills/ directory
Copier





Maison
