【问题标题】:Python - download google sheet - to csv filePython - 下载谷歌表 - 到 csv 文件
【发布时间】:2021-09-09 10:40:07
【问题描述】:

我正在寻找一种在我的计算机上将 google 工作表另存为 csv 的方法。 我试过这个:

import gspread

gc = gspread.service_account(filename='client_secret.json')
sh = gc.open("sheets").worksheet("sheet1")

sh.to_csv("exported_file.csv")

如何让它发挥作用?

【问题讨论】:

  • 你遇到了什么错误?

标签: python google-sheets export-to-csv google-sheets-api gspread


【解决方案1】:

我建议为此使用gsheetsoauth2client 模块。

from oauth2client.service_account import ServiceAccountCredentials
import gsheets

my_json = "client_secret.json"
my_sheet_url = f"https://docs.google.com/spreadsheets/d/{insert_id}"

SCOPE = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
         "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
CREDS = ServiceAccountCredentials.from_json_keyfile_name(my_json, SCOPE)

sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(my_sheet_url)
sheet[0].to_csv("export.csv")

这会将第一个工作表保存为 .py 文件旁边的 csv 文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2020-05-22
    • 2016-07-10
    • 1970-01-01
    相关资源
    最近更新 更多