如何使用Python从Google云端硬盘以管道分隔格式下载Google表格(非工作簿)

时间:2016-12-13 15:59:04

标签: python-3.x google-drive-api google-api-python-client google-drive-realtime-api google-sheets-api

我正在编码使用google drive api从google驱动器下载文件到本地系统。以下是我的代码。我有以下问题:

1)有没有办法指定只下载工作簿或完整工作簿的工作表编号?

2)我们有MIMETYPE'text / csv',有没有办法用其他分隔符说明管道?

3)我们可以指定下载位置吗?现在正在下载python脚本的地方。

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file , client , tools
import io
from apiclient.http import MediaIoBaseDownload


try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

#Set the scope for authorization and specify json file    
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET = 'client_secret.json'

#Atleast once we will have to allow our program to access document, after that it would be stored in storage.json file
store = file.Storage('storage.json')
credz = store.get()
if not credz or credz.invalid:
    flow = client.flow_from_clientsecrets(CLIENT_SECRET , SCOPES)
    credz = tools.run_flow(flow,store,flags)\
            if flags else tools.run(flow,store)
DRIVE = build('drive','v2',http=credz.authorize(Http()))

MIMETYPE='text/csv'
file_id='1p3yRgi093TKbsBrxkUkV1cP-6h8dWUIKXycU62i9Arc'
request = DRIVE.files().export_media(fileId=file_id,mimeType=MIMETYPE)

fh = io.FileIO('Google App Scripts for beginner.csv','wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print ("Download %d%%." % int(status.progress() * 100))

0 个答案:

没有答案