azure-ai-translation-document-py
microsoft/skills
Übersetzen Sie Word-, PDF-, Excel-, PowerPoint- und andere Dokumente in großem Umfang mithilfe des Azure AI Document Translation SDK unter Beibehaltung des Formats.
...Alle erweiternAzure AI Document Translation SDK für Python
Client-Bibliothek für den Azure AI Translator-Dienst zur Dokumentübersetzung für die Stapelübersetzung von Dokumenten unter Beibehaltung des Formats.
Installation
pip install azure-ai-translation-document
Umgebungsvariablen
AZURE_DOCUMENT_TRANSLATION_ENDPOINT=https://.cognitiveservices.azure.com # Erforderlich für alle Authentifizierungsmethoden
# Speicherort für Quell- und Zieldokumente
AZURE_SOURCE_CONTAINER_URL=https://.blob.core.windows.net/? # Erforderlich für alle Authentifizierungsmethoden
AZURE_TARGET_CONTAINER_URL=https://.blob.core.windows.net/? # Erforderlich für alle Authentifizierungsmethoden
AZURE_TOKEN_CREDENTIALS=prod # Nur erforderlich, wenn „DefaultAzureCredential“ in der Produktion verwendet wird
AZURE_DOCUMENT_TRANSLATION_KEY= # Nur erforderlich für den unten aufgeführten Authentifizierungspfad mit dem alten API-Schlüssel
Authentifizierung und Lebenszyklus
🔑 Für alle folgenden Code-Beispiele gelten zwei Regeln:
- Verwenden Sie vorzugsweise
„DefaultAzureCredential“. Es funktioniert lokal (Azure CLI / VS Code / Developer CLI) und in Azure (verwaltete Identität, Workload-Identität) ohne Codeänderungen. Vermeiden Sie Verbindungszeichenfolgen, Konten- und API-Schlüssel – diese umgehen die Entra-Überwachung und -Rotation.
- Lokale Entwicklung:
„DefaultAzureCredential“funktioniert so wie es ist.- Produktion: Setzen Sie
AZURE_TOKEN_CREDENTIALS=prod(oderAZURE_TOKEN_CREDENTIALS=), um die Anmeldeketten auf produktionssichere Anmeldeinformationen zu beschränken.- Hüllen Sie jeden Client in einen Kontextmanager ein, damit HTTP-Transporte, Sockets und Token-Caches deterministisch freigegeben werden:
- Synchron:
mit `(...)` als Client: - Asynchron:
async mitund(...) als Client: async mit DefaultAzureCredential() als Anmeldeinformationen:(ausazure.identity.aio)Code-Schnipsel können diese Konfiguration zwar verkürzen, aber Produktionscode sollte stets beide Regeln befolgen.
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.ai.translation.document import DocumentTranslationClient
# Lokale Entwicklung: DefaultAzureCredential. Produktion: Setze AZURE_TOKEN_CREDENTIALS=prod oder AZURE_TOKEN_CREDENTIALS=
credential = DefaultAzureCredential(require_envvar=True)
# Oder verwenden Sie in der Produktion direkt eine bestimmte Anmeldeinformation:
# Siehe 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())
Alte Methode: API-Schlüssel (bestehende schlüsselbasierte Bereitstellungen)
Neuer Code sollte die oben genannte `DefaultAzureCredential` verwenden. Verwenden Sie `AzureKeyCredential` nur, wenn Sie über eine bestehende schlüsselbasierte Bereitstellung verfügen, die noch nicht zu Entra ID migriert wurde – beispielsweise in regulierten Umgebungen, in denen die Einführung von Entra noch nicht abgeschlossen ist.
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“ akzeptiert dieselben schlüsselbasierten Anmeldeinformationen.
Einfache Dokumentübersetzung
from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget
source_url = os.environ["AZURE_SOURCE_CONTAINER_URL"]
target_url = os.environ["AZURE_TARGET_CONTAINER_URL"]
# Übersetzungsauftrag starten
poller = client.begin_translation(
inputs=[
DocumentTranslationInput(
source_url=source_url,
targets=[
TranslationTarget(
target_url=target_url,
language="es" # Nach Spanisch übersetzen
)
]
)
]
)
# Auf Abschluss warten
result = poller.result()
print(f"Status: {poller.status()}")
print(f"Übersetzte Dokumente: {poller.details.documents_succeeded_count}")
print(f"Fehlgeschlagene Dokumente: {poller.details.documents_failed_count}")
Mehrere Zielsprachen
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")
]
)
]
)
Einzelnes Dokument übersetzen
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"
)
# Übersetztes Dokument speichern
with open("document_es.docx", "wb") as f:
f.write(result)
Übersetzungsstatus prüfen
# Alle Übersetzungsvorgänge abrufen
operations = client.list_translation_statuses()
for op in operations:
print(f"Vorgangs-ID: {op.id}")
print(f"Status: {op.status}")
print(f"Erstellt: {op.created_on}")
print(f"Gesamtzahl der Dokumente: {op.documents_total_count}")
print(f"Erfolgreich: {op.documents_succeeded_count}")
print(f"Fehlgeschlagen: {op.documents_failed_count}")
Dokumentstatus auflisten
# Status einzelner Dokumente in einem Auftrag abrufen
operation_id = poller.id
document_statuses = client.list_document_statuses(operation_id)
for doc in document_statuses:
print(f"Dokument: {doc.source_document_url}")
print(f" Status: {doc.status}")
print(f" Übersetzt in: {doc.translated_to}")
if doc.error:
print(f" Fehler: {doc.error.message}")
Übersetzung abbrechen
# Eine laufende Übersetzung abbrechen
client.cancel_translation(operation_id)
Glossar verwenden
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"
)
]
)
]
)
]
)
Unterstützte Dokumentformate
# Unterstützte Formate abrufen
formats = client.get_supported_document_formats()
for fmt in formats:
print(f"Format: {fmt.format}")
print(f" Dateiendungen: {fmt.file_extensions}")
print(f" Inhaltstypen: {fmt.content_types}")
Unterstützte Sprachen
# Unterstützte Sprachen abrufen
languages = client.get_supported_languages()
for lang in languages:
print(f"Sprache: {lang.name} ({lang.code})")
Asynchroner Client
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()
Unterstützte Formate
| Kategorie | Formate |
|---|---|
| Dokumente | DOCX, PDF, PPTX, XLSX, HTML, TXT, RTF |
| Strukturiert | CSV, TSV, JSON, XML |
| Lokalisierung | XLIFF, XLF, MHTML |
Speicheranforderungen
- Quell- und Zielcontainer müssen Azure Blob Storage sein
- Verwenden Sie SAS-Token mit entsprechenden Berechtigungen:
- Quelle: Lesen, Auflisten
- Ziel: Schreiben, Auflisten
Bewährte Vorgehensweisen
- Entscheiden Sie sich für „sync“ ODER „async“ und bleiben Sie dabei. Mischen Sie keine
„azure.xxx“-Sync-Clients mit„azure.xxx.aio“-Async-Clients im selben Aufrufpfad. Wählen Sie pro Modul einen Modus. - Verwenden Sie für Clients und asynchrone Anmeldeinformationen stets Kontextmanager. Umschließen Sie jeden Client
mit `Client(...) as client:` (synchron) oder`Client(...) as client:`(asynchron). Verwenden Sie für asynchrone`DefaultAzureCredential` aus`azure.identity.aio` ebenfalls„async“ mit `credential:`, damit Tokens und Transporte bereinigt werden. - Verwenden Sie SAS-Token mit den minimal erforderlichen Berechtigungen
- Überwachen Sie lang andauernde Vorgänge mit `
poller.status()` - Behandeln Sie Fehler auf Dokumentebene, indem Sie Dokumentstatus durchlaufen
- Verwenden Sie Glossare für domänenspezifische Terminologie
- Trennen Sie die Zielcontainer nach Sprachen
- Verwenden Sie den Async-Client für mehrere gleichzeitige Aufträge
- Überprüfen Sie vor dem Einreichen von Dokumentendie unterstützten Formate
---
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
Alle Dateien
0 Dateienazure-ai-translation-document-py installieren
Laden Sie die Skill-Dateien herunter und entpacken Sie sie in Ihr Verzeichnis „.claude/skills/“.
ZIP herunterladenKlonen Sie das Repository und kopieren Sie die Skill-Dateien in Ihr Projekt.
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
Kopieren





Heim
