azure-ai-translation-document-py について
Azure AI Translator のドキュメント翻訳サービスを利用する Python クライアントライブラリです。書式を保った一括翻訳を必要とする開発者向けで、複数言語、非同期ジョブ、状態確認、用語集、単一文書の翻訳に対応します。
主な機能
- 01書式を保った文書の一括翻訳
- 02ジョブごとに複数の翻訳先を指定
- 03ジョブと文書単位の状態確認
- 04実行中の翻訳のキャンセル
- 05用語集を翻訳に適用
- 06対応形式と言語の一覧取得
活用シーン
- 01Azure Blob Storage の文書を一括翻訳
- 02文書群から複数言語版を作成
- 03長時間ジョブで失敗した文書を特定
- 04分野別の用語集を使って業務文書を翻訳
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-ai-translation-document-pyインストール後、Claude Code で/azure-ai-translation-document-pyを入力して読み込まれたか確認します。
詳細
Python 用 Azure AI Document Translation SDK
Azure AI Translator ドキュメント翻訳サービス用のクライアントライブラリです。書式を保持したまま、ドキュメントを一括翻訳できます。
インストール
pip install azure-ai-translation-document
環境変数
AZURE_DOCUMENT_TRANSLATION_ENDPOINT=https://<resource>.cognitiveservices.azure.com
AZURE_DOCUMENT_TRANSLATION_KEY=<your-api-key> # If using API key
# Storage for source and target documents
AZURE_SOURCE_CONTAINER_URL=https://<storage>.blob.core.windows.net/<container>?<sas>
AZURE_TARGET_CONTAINER_URL=https://<storage>.blob.core.windows.net/<container>?<sas>
認証
API キー
import os
from azure.ai.translation.document import DocumentTranslationClient
from azure.core.credentials import AzureKeyCredential
endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
Entra ID(推奨)
from azure.ai.translation.document import DocumentTranslationClient
from azure.identity import DefaultAzureCredential
client = DocumentTranslationClient(
endpoint=os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"],
credential=DefaultAzureCredential()
)
情報開示とコストの境界
翻訳ではドキュメントが Azure に送信され、選択したコンテナーに出力が書き込まれます。許可されたドキュメントの範囲、保存先、リージョン、保持期間、予算を確認してください。SAS URL をログに記録せず、短期間で有効期限が切れる最小権限のアクセスを使用し、各ドキュメントの失敗を個別に処理してください。ジョブ全体の完了は、すべてのドキュメントの成功を意味するものではありません。SDK/単一ドキュメントのメソッドや言語・形式のサポートはバージョンに依存します。コード例を実行する前に、最新のパッケージドキュメントを確認してください。
基本的なドキュメント翻訳
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}")
複数の対象言語
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")
]
)
]
)
単一ドキュメントの翻訳
from azure.ai.translation.document import SingleDocumentTranslationClient
single_client = SingleDocumentTranslationClient(endpoint, AzureKeyCredential(key))
with open("document.docx", "rb") as f:
document_content = f.read()
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)
翻訳ステータスの確認
# 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}")
ドキュメントのステータス一覧表示
# 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 ID: {doc.id}") # Avoid logging SAS-bearing source URLs.
print(f" Status: {doc.status}")
print(f" Translated to: {doc.translated_to}")
if doc.error:
print(f" Error: {doc.error.message}")
翻訳のキャンセル
# Cancel a running translation
client.cancel_translation(operation_id)
用語集の使用
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"
)
]
)
]
)
]
)
サポートされているドキュメント形式
# 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}")
サポートされている言語
# Get supported languages
languages = client.get_supported_languages()
for lang in languages:
print(f"Language: {lang.name} ({lang.code})")
非同期クライアント
from azure.ai.translation.document.aio import DocumentTranslationClient
from azure.identity.aio import DefaultAzureCredential
async def translate_documents():
async with DocumentTranslationClient(
endpoint=endpoint,
credential=DefaultAzureCredential()
) as client:
poller = await client.begin_translation(inputs=[...])
result = await poller.result()
サポートされている形式
| カテゴリ | 形式 |
|---|---|
| ドキュメント | DOCX, PDF, PPTX, XLSX, HTML, TXT, RTF |
| 構造化データ | CSV, TSV, JSON, XML |
| ローカライズ | XLIFF, XLF, MHTML |
ストレージ要件
- ソースコンテナーと対象コンテナーには Azure Blob Storage を使用する必要があります
- 適切な権限を持つ SAS トークンを使用してください:
- ソース:読み取り、一覧表示
- 対象:書き込み、一覧表示
ベストプラクティス
- 必要最小限の権限を持つ SAS トークンを使用する
poller.status()で 長時間実行される操作を監視する- ドキュメントのステータスを反復処理して ドキュメント単位のエラーを処理する
- 分野固有の用語には 用語集を使用する
- 言語ごとに 対象コンテナーを分ける
- 複数のジョブを同時に実行する場合は 非同期クライアントを使用する
- ドキュメントを送信する前に サポートされている形式を確認する
使用する場合
このスキルは、概要で説明されているワークフローまたはアクションを実行する場合に適用できます。
制限事項
- このスキルは、タスクが上記の説明の範囲に明確に該当する場合にのみ使用してください。
- 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
- 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、作業を中止して確認を求めてください。
コメント
Markdown 対応