繁体   English   中英

使用 Python 和 GSpread 将电子表格下载到 CSV

[英]Download spreadsheet to CSV with Python and GSpread

使用此代码,我可以将电子表格中的所有工作表下载到 csv。 但我只想下载一张表。 有没有办法呢?

代码

import csv
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)

docid = "123"

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(docid)
for i, worksheet in enumerate(spreadsheet.worksheets()):
    filename = docid + '-worksheet' + str(i) + '.csv'
    with open(filename, 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(worksheet.get_all_values())

如果有人知道,请告诉我。

从这些文件中我获得了信息

使用按名称获取工作表的方法应该适合您。

示例(仅打印“sheet2”):

import csv
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)

docid = "123"

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(docid)
worksheetName = "sheet2"
worksheet = spreadsheet.worksheet(worksheetName)
filename = docid + '-worksheet' + worksheetName + '.csv'
with open(filename, 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(worksheet.get_all_values())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM