googleapi的高级python接口
googleA的Python项目详细描述
谷歌API
这是一个python包,它处理Google服务的所有工作,包括googledrive、googlesheet和Gmail。它是Google API的高级接口。直接使用googleapi的缺点是它的文档是用python2.x编写的,这使得它很难理解。在
要使用googleapi,第一步是获得凭证.json'. 最简单的方法是访问Google Drive API Quickstart,然后单击“步骤1:打开驱动器API”部分中的Enable the Drive API
按钮。那么沃利娅,给你!不需要处理项目,服务帐户等
这个包使用Drive API v3,Google Sheets API v4,Gmail API v1。在
安装
pip install googleAPI
依赖关系
- googleapi python客户端
- google-auth-httplib2
- 谷歌认证oauthlib
文件
凭证
每个API都需要凭据。因此,GoogleCredential()
是所有其他类的基类。在
注意:除非您想编写自定义函数,否则可以跳过此部分。
假设我们想获得Google驱动器凭证,其中包含编写定制函数的所有范围。在
# Step 1: import the modulefromgoogleAPI.credentialimport*# Step 2: Instance `GoogleCredential()` classgc=GoogleCredential()# Step 3: Get credential# `credential_path` is the place where 'credentials.json' is stored.# There will be a prompt web page that will download the 'token.pickle'# into `credential_path`.creds=gc.credential(credential_path='',credential_scopes=['https://www.googleapis.com/auth/drive'])
参数credential_scopes
是必需的。对于您可以选择的所有范围,请访问OAuth 2.0 Scopes for Google APIs。在
GoogleDrive
googledrive是一个云存储,您可以在其中存储文件和文件夹。GoogleDrive()
允许您搜索文件ID、获取文件元数据、下载文件等
假设我们想从Google驱动器下载一个.csv文件。在
^{pr2}$GoogleSheet
假设我们要下载一个名为Google Sheet 1
的GoogleSheet作为microsoftexcel文件Google Sheet 1.xlsx
。在
# Step 1: import the modulefromgoogleAPI.sheetimport*# Step 2: Instance `GoogleSheet` class with credentialgs=GoogleSheet(credential_path='')# Step 3: Find the Google Sheet ID using spreadsheet name# The result of the `GoogleSheet.search_spreadsheet()` is# saved in a list inside a dictionary in line with the# `GoogleDrive.search_files()`spreadsheet_id=gs.search_spreadsheet("Google Sheet 1")["Google Sheet 1"][0]# Step 4: Download the Google Sheetgs.download_spreadsheet(spreadsheet_id,save_as="Google Sheet 1.xlsx")
假设我们已经有了电子表格ID,我们只想得到这些值
从特定的页Sheet1
。在
values=gs.get_values(spreadsheet_id,range_="'Sheet1'")
- 项目
标签: