-2
如何複製/使用python腳本,請您及時與共享一些例子如何複製/使用python腳本
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
CSV文件上傳到谷歌電子表格的CSV文件上傳到谷歌電子表格
憑證= get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl =(」 https://sheets.googleapis.com/ $發現/休息嗎? '' 版本= V4' )
打印(憑證)
服務= discovery.build( '片', 'V4',憑證=憑證)
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)
[這](https://pypi.python.org/pypi/PyDrive)做的是我在工作中使用。進行一些代碼嘗試。 –
嗨,因爲我是新的python搜索相同的。目前我已經嘗試寫在谷歌sheet.But我需要複製/上傳CSV電子表格文件數據到谷歌。 –
[按照步驟](http://pythonhosted.org/PyDrive/filemanagement.html),您需要首先設置您的OAuth訪問權限,只需按照說明操作即可。 –