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

如何用Python调用SYNO.FileStation.Download API将OSHEET转XLSX下载

问题

尝试使用Python调用SYNO.FileStation.Download API,从群晖FileStation将OSHEET类型文件下载为XLSX格式。当前代码如下:

response = requests.get(myURL+
   "/webapi/entry.cgi/?api=SYNO.FileStation.Download&version=2&method=download&path="
   + pathToFile + "&mode=download&_sid="+sid, allow_redirects=True)

output = open('test.xlsx', 'wb')
output.write(response.content)
output.close()

执行脚本后生成了test.xlsx文件,但无法打开,提示错误:Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid。已知群晖NAS的OSHEET文件可转换为XLSX格式(已通过Web UI完成操作),请问如何通过Python将API返回的响应对象转换为有效的XLSX文件?

解决方法

你当前的API调用只是直接下载了OSHEET原文件,并未触发群晖的格式转换逻辑。Web UI里的格式转换需要通过特定参数触发,以下是正确实现步骤:

  • 添加格式转换参数:在API请求中加入format=xlsx参数,明确要求将OSHEET转换为XLSX格式后下载。推荐用params字典传递参数,自动处理URL编码,避免特殊字符导致的请求错误:
response = requests.get(
    f"{myURL}/webapi/entry.cgi/",
    params={
        "api": "SYNO.FileStation.Download",
        "version": "2",
        "method": "download",
        "path": pathToFile,
        "mode": "download",
        "_sid": sid,
        "format": "xlsx"  # 关键:指定转换为XLSX格式
    },
    allow_redirects=True
)
  • 验证响应有效性:请求完成后,可检查响应的Content-Type是否为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet(XLSX标准MIME类型),确认转换是否成功:
print(response.headers.get('Content-Type'))
  • 优化文件写入:使用with语句自动管理文件句柄,避免资源泄漏:
with open('test.xlsx', 'wb') as output:
    output.write(response.content)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 06:57:38