gpt4 book ai didi

python - Gspread 将电子表格导出到带格式的文件系统

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:19 27 4
gpt4 key购买 nike

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

我尝试导出为 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")

最佳答案

尝试导出到 .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()

关于python - Gspread 将电子表格导出到带格式的文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com