谷歌套件应用程序的数据科学家工具箱
pysuite的Python项目详细描述
pysuite:googlesuite应用程序的数据科学家工具箱
一个用于googlesuiteapi的python包装器。这为一些类提供了用户友好的api,以便与googlesuite一起操作 谷歌硬盘和谷歌电子表格等应用程序。在
获取凭据
你需要从 Google API Console。凭证看起来像:
{"installed":{"client_id":"xxxxxxxxxxxxxxxxx.apps.googleusercontent.com","project_id":"xxxxxxxxxxxxx-xxxxxxxxxxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxxxxxxxxxxxxxx","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
如果可能,您还可以提供一个令牌json文件,令牌文件如下所示:
^{pr2}$如果令牌文件不存在,则需要在浏览器提示下进行确认。然后将创建令牌文件。在
frompysuiteimportAuthenticationcredential_json_file="/tmp/credential.json"token_path_file="/tmp/token.json"client=Authentication(credential=credential_json_file,token=token_path_file,services="sheets")
认证
Authentication
blass可以帮助验证您的凭据并为API提供服务客户端。例如“驾驶”和
“床单”。在
frompysuiteimportAuthenticationcredential_file="./credentials/credentials.json"token_file="./credentials/token.json"drive_auth=Authentication(credential=credential_file,token=token_file,services="drive")sheets_auth=Authentication(credential=credential_file,token=token_file,services="sheets")
现在可以从身份验证对象生成gdrive客户端。在
service=drive_auth.get_service_client()# 'service' needed if not provided when initiating Authenciation object
美国石油学会
API类的目的是提供对Google套件应用程序(如googledrive和Google电子表格)的快速简单访问。在
驱动
frompysuiteimportDrivedrive=Drive(service=drive_auth.get_service_client())# drive_auth is an Authenticaion class with `service='drive'`
如果您喜欢使用不同的方法来创建gdrive客户机,可以使用gdrive服务切换drive_auth.get_service()
(详见Google Drive API V3):
service=build('drive','v3',credentials=creds)
一些示例API如下所示:
下载文件
drive.download(id="google drive object id",to_file="/tmp/test_file")
上传文件
drive.upload(from_file="path/to/your/file/to/be/uploaded",name="google_drive_file_name",parent_ids=["google drive folder id 1","google drive folder id 2"])
列出文件夹中的文件
list_of_objects=drive.list(id="google drive folder id")
表
frompysuiteimportSheetssheets=Sheets(service=sheets_auth.get_service_client())# sheets_auth is an Authenticaion class with `service='sheets'`
如果您喜欢使用不同的方法来创建gdrive客户机,可以使用gsheet服务切换sheets_auth.get_service()
(详见Google Sheet API V4):
service=build('sheets','v4',credentials=creds,cache_discovery=True)
以下是一些例子:
上传熊猫数据帧
importpandasaspddf=pd.DataFrame({"col1":[1,2],"col2":['a','b']})sheets.to_sheet(df,id="your_sheet_id",sheet_range="yourtab!A1:B")
将工作表下载到dataframe
此api需要熊猫。在
df=sheets.read_sheet(id="your_sheet_id",sheet_range="yourtab!A1:D")
- 项目
标签: