3

我需要在文件系统上下载 Google 电子表格,我正在使用 Gspread 从 Google Drive 中读取文件,效果很好。

我尝试导出为 CSV,但这当然会丢弃格式。

    json_key = json.load(open('/googleDriveCredentials.json'))
    scope = ['https://spreadsheets.google.com/feeds']
    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
    gc = gspread.authorize(credentials)
    wks = gc.openall()

    # processing first workbook
    spreadsheet = wks[1] 

    # Creating CSV file which doesn't preserve formatting
    for i, worksheet in enumerate(spreadsheet.worksheets()):
        filename = 'somefile' + str(i) + '.csv'
        with open(filename, 'wb') as f:
            writer = csv.writer(f)
            writer.writerows(worksheet.get_all_values())  

我想知道我是否可以做这样的事情

worksheet.export("/myFolder")
4

1 回答 1

1

尝试导出到 .XLSX 文件。格式将被保留:

import gspread

gc = gspread.authorize(credentials)
sh = gc.open_by_url('your spreadsheet')
worksheet = sh.worksheet('your worksheet')
export_file = worksheet.export(format='xlsx')
f = open('filename.xlsx', 'wb')
f.write(export_file)
f.close()
于 2017-05-26T14:13:45.283 回答