在 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. 版权所有。