翻譯 Python 中的 DOCX

使用 AI 翻譯 DOCX 的 Python 解決方案

與 Google Gemini 生成式語言模型的無縫整合,為 Python 應用程式提供廣泛的 AI DOCX 處理功能。

查看代碼片段

在 Python 中將 DOCX 文件翻譯成 300 多種語言

使用 Google Gemini AI 模型與 Aspose.Words for Python via .NET 自動化 DOCX 文件翻譯。將 DOCX 檔案翻譯成超過 300 種語言,同時保留原始版面、格式與文件結構。

如何在 Python 中翻譯 DOCX 檔案

  • 將 DOCX 檔案載入 Document 物件
  • 使用您的 API 金鑰設定 Google Gemini AI 模型
  • 呼叫 Translate 方法,並指定目標語言
  • 將翻譯後的文件儲存為您需要的格式

AI 引擎會自動偵測來源語言——即使在混合語言的文件中亦是如此。以下範例示範如何在 Python 中翻譯 DOCX 文件:

使用 Python 的範例程式碼,示範翻譯 DOCX
從列表中選擇目標格式
運行代碼
pip install aspose-words
複製
doc = aw.Document("Document.docx")
api_key = os.getenv("API_KEY")

# 使用 Google 生成式語言模型。
model = aw.ai.AiModel.create(aw.ai.AiModelType.GEMINI_FLASH_LATEST).with_api_key(api_key)

translated_doc = model.translate(doc, aw.ai.Language.ARABIC)
translated_doc.save("Output.docx")
doc = aw.Document("Document.docx")
# Pass an empty string if the self-hosted model does not require an API key.
model = CustomAiModel("my-model-24b", "https://your-server.com/v1").with_api_key("")

translated_doc = model.translate(doc, aw.ai.Language.ARABIC)
translated_doc.save("Output.docx")

class CustomAiModel(aw.ai.OpenAiModel):
    def __init__(self, name, url):
        super().__init__(name)
        self._url = url
    @property
    def url(self):
        return self._url
運行代碼

如何翻譯 Python 中的 DOCX

  1. 安裝 Aspose.Words for Python via .NET。
  2. 將函式庫參考(匯入函式庫)加入您的 Python 專案。
  3. 在 Python 中開啟來源檔案。
  4. 呼叫 Translate 方法,傳入您的文件與目標語言。
  5. 完成後,您將取得翻譯好的文件,您可以將其另存為所需格式的新檔案。

Python via .NET 函式庫,用於翻譯 DOCX 檔案

我們在PyPi存儲庫中託管我們的 Python 請按照有關如何 "Aspose.Words for Python via .NET" 安裝到您的開發人員環境的分步說明進行操作。

系統要求

這個包與 Python ≥3.5 和 <3.12 兼容。如果您為 Linux 開發軟件,請查看產品文檔中對 gcc 和 libpython 的附加要求。

5%

訂閱 Aspose 產品更新

獲取直接發送到您的郵箱的每月通訊和優惠。

© Aspose Pty Ltd 2001-2026. 版權所有。