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

如何将.xlsx文件转换为Google Sheets格式?遇APIError求助

解决gspread读取Excel转Google Sheets时的400错误

错误原因

你遇到的APIError: {'code': 400, 'message': 'This operation is not supported for this document', 'status': 'FAILED_PRECONDITION'},核心原因是当前操作的文档并非原生Google Sheets格式——它只是上传到Google Drive的Excel文件,仅处于预览状态,gspread仅支持操作原生Google Sheets文档。

解决方法

方法1:手动转换(适合单次操作)

  • 打开目标文档的Google Drive链接
  • 点击顶部菜单栏的「文件」→「保存为Google Sheets」
  • 复制新生成的原生Google Sheets文档的URL,替换代码中的原链接后重新运行

方法2:自动转换(适合批量操作)

使用Google Drive API将上传的Excel文件批量转换为原生Google Sheets,示例代码如下:

from googleapiclient.discovery import build
from google.auth import default
import gspread

# 初始化授权
creds, _ = default()
drive_service = build('drive', 'v3', credentials=creds)
gc = gspread.authorize(creds)

# 替换为你的Excel文件ID(从URL的d/后提取)
file_id = '19vWu-OSjAKSvfh8FgvZClk4aGsGxx5ej'

# 执行转换
convert_request = drive_service.files().copy(
    fileId=file_id,
    body={
        'name': '转换后的表格副本',
        'mimeType': 'application/vnd.google-apps.spreadsheet'
    }
)
converted_sheet = convert_request.execute()

# 用转换后的文档进行操作
new_sheet_url = f"https://docs.google.com/spreadsheets/d/{converted_sheet['id']}/edit"
workbook = gc.open_by_url(new_sheet_url)
sheet = workbook.get_worksheet(0)
list_of_lists = sheet.get_all_values()

print(list_of_lists)

注意事项

  • 确保授权账号拥有目标Excel文件的编辑权限,否则无法完成转换
  • 转换操作会生成新的Google Sheets文档,原Excel文件不会被修改

内容的提问来源于stack exchange,提问作者Laura C Moncada Castro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 04:20:26