使用 Google Gemini AI 模型和 Aspose.Words for Python via .NET 自动化 DOCX 文档翻译。将 DOCX 文件翻译成超过 300 种语言,同时保留原始布局、格式和文档结构。
AI 引擎会自动检测源语言——即使在混合语言文档中也是如此。以下示例展示了如何在 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
我们在PyPi存储库中托管我们的 Python 请按照有关如何 "Aspose.Words for Python via .NET" 安装到您的开发人员环境的分步说明进行操作。
这个包与 Python ≥3.5 和 <3.12 兼容。如果您为 Linux 开发软件,请查看产品文档中对 gcc 和 libpython 的附加要求。