使用 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 的附加要求。