更新教程
This commit is contained in:
53
README.md
53
README.md
@@ -252,7 +252,58 @@ async def main():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
```
|
```
|
||||||
### 示例 3: 翻译一个 xlsx 文件 (使用 `XlsxWorkflow`)
|
|
||||||
|
### 示例 4: 翻译一个 docx 文件 (使用 `DocxWorkflow`)
|
||||||
|
|
||||||
|
这里以异步方式为例。
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from docutranslate.exporter.docx.docx2html_exporter import Docx2HTMLExporterConfig
|
||||||
|
from docutranslate.translator.ai_translator.docx_translator import DocxTranslatorConfig
|
||||||
|
from docutranslate.workflow.docx_workflow import DocxWorkflowConfig, DocxWorkflow
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
# 1. 构建翻译器配置
|
||||||
|
translator_config = DocxTranslatorConfig(
|
||||||
|
base_url="https://api.openai.com/v1/",
|
||||||
|
api_key="YOUR_OPENAI_API_KEY",
|
||||||
|
model_id="gpt-4o",
|
||||||
|
to_lang="中文",
|
||||||
|
insert_mode= "replace",#备选项 "replace", "append", "prepend"
|
||||||
|
separator = "\n",# "append", "prepend"模式时使用的分隔符
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. 构建主工作流配置
|
||||||
|
workflow_config = DocxWorkflowConfig(
|
||||||
|
translator_config=translator_config,
|
||||||
|
html_exporter_config=Docx2HTMLExporterConfig(cdn=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 3. 实例化工作流
|
||||||
|
workflow = DocxWorkflow(config=workflow_config)
|
||||||
|
|
||||||
|
# 4. 读取文件并执行翻译
|
||||||
|
workflow.read_path("path/to/your/notes.docx")
|
||||||
|
await workflow.translate_async()
|
||||||
|
# 或者使用同步的方法
|
||||||
|
# workflow.translate()
|
||||||
|
|
||||||
|
# 5. 保存结果
|
||||||
|
workflow.save_as_docx(name="translated_notes.docx")
|
||||||
|
print("docx文件已保存。")
|
||||||
|
|
||||||
|
# 也可以导出翻译后的docx的二进制
|
||||||
|
text_bytes = workflow.export_to_docx()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
|
```
|
||||||
|
|
||||||
|
### 示例 5: 翻译一个 xlsx 文件 (使用 `XlsxWorkflow`)
|
||||||
|
|
||||||
这里以异步方式为例。
|
这里以异步方式为例。
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user