You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Google Cloud Translation API返回的翻译文档写入本地文件

问题解决方法

response.document_translation.byte_stream_outputs返回的Repeated类型是类列表的复合容器,内部存储的每个元素才是可写入的字节对象,单文档翻译场景下容器内通常只有1个字节片段,直接取首个元素写入即可:

with open('translated_file.txt', 'wb') as f:     
    f.write(response.document_translation.byte_stream_outputs[0])    

如果是多文件批量翻译或者返回多段字节流的场景,遍历容器逐个写入即可:

with open('translated_file.txt', 'wb') as f:
    for segment in response.document_translation.byte_stream_outputs:
        f.write(segment)

注意:如果你的原始待翻译文件是docx、pdf、xlsx等非纯文本格式,保存翻译结果时后缀需要和原始文件保持一致,否则会出现文件无法正常打开的问题。

内容的提问来源于stack exchange,提问作者John Beavers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 21:54:03