如何使用python脚本将csv文件复制/上传到谷歌电子表格。请与一些例子分享
from __future__ import print_function
from pprint import pprint
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.service_account import ServiceAccountCredentials
from apiclient import discovery
from googleapiclient import discovery
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() except ImportError:flags = None
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Sheets API Python Quickstart'
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'sheets.googleapis.com-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl =('https://sheets.googleapis.com/ $ discovery / rest?''version = v4')
打印(凭证)
service = discovery.build('sheets','v4',credentials = credentials)
spreadsheet_id = '1oH2nTJblDvcIVySuBzIXJF-Np3xsCBkCqTDkg9TCBrc'
range_ = 'Sheet1!A1:B4'
value_input_option ='RAW'#TODO:更新占位符值。
insert_data_option = 'INSERT_ROWS'
f= open('/home/vinaykn/Documents/test2.csv')
data= f.read()
f.close()
get_data = data
print (get_data)
value_range_body = {
"values":
["Item", "Cost", "Stocked", "Ship Date"],
["Wheel", "$20.50", "4", "3/1/2016"],
["Door", "$15", "2", "3/15/2016"],
["Engine", "$100", "1", "30/20/2016"]
}
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
response = request.execute()
pprint(response)
答案 0 :(得分:0)
经过多次调查后,我找到了答案。
http://wescpy.blogspot.in/2015/12/google-drive-uploading-downloading.html
这应该能够上传或下载