Authentication code creation
from google.colab import auth
auth.authenticate_user()
Preparation for drive operation with pydrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.client import GoogleCredentials
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
Please refer to the following when executing without authentication work. https://qiita.com/plumfield56/items/3d9e234366bcaea794ac
Once you are ready for the above state, run the search below.
python
drive.ListFile({'q':"{Describe the contents of query}"})
For example, Title is Hello
and mimeType is Spreadsheet
, Other than Trash
.
When searching, describe as follows.
python
drive.ListFile({'q':"title='Hello' and mimeType='application/vnd.google-apps.spreadsheet' and trashed = false"})
For other search examples, refer to the following site https://developers.google.com/drive/api/v2/search-files#query_string_examples
Use the following to make detailed specifications with query.
the term | Available comparison operators | how to use |
---|---|---|
title | contains, =, != | Search for title Escape with backspace when using single quotes |
fullText | contains | Search for title, description, content, index |
mimeType | contains, =, != | Search for MIME type |
modifiedDate | <=, <, =, !=, >, >= | Search for last updated date |
lastViewedByMeDate | <=, <, =, !=, >, >= | Search by last viewed date |
trashed | =, != | Specify whether to search in the trash(true,Specified by false) |
starred | =, != | Specify whether to search for items with favorites(true,Specified by false) |
parents | in | Specify parent ID |
owners | in | Specify owner |
writers | in | Specifies whether a particular user is an editor |
readers | in | Specify whether a specific user is a viewer |
See the site below https://developers.google.com/drive/api/v2/ref-search-terms
Service name | MIME Types |
---|---|
Google Drive file | application/vnd.google-apps.file |
Google Drive folder | application/vnd.google-apps.folder |
Google Docs | application/vnd.google-apps.document |
Google Sheets | application/vnd.google-apps.spreadsheet |
Google Slides | application/vnd.google-apps.presentation |
Google Sites | application/vnd.google-apps.site |
Google Forms | application/vnd.google-apps.form |
Google Drawing | application/vnd.google-apps.drawing |
Google Fusion Tables | application/vnd.google-apps.fusiontable |
Google My Maps | application/vnd.google-apps.map |
Google Apps Scripts | application/vnd.google-apps.script |
Shortcut | application/vnd.google-apps.shortcut |
application/vnd.google-apps.photo |
See the site below https://developers.google.com/drive/api/v2/mime-types
file format | MIME Types |
---|---|
application/pdf | |
JSON | application/vnd.google-apps.script+json |
Plain text | text/plain |
HTML | text/html |
MS Excel | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
MS PowerPoint | application/vnd.openxmlformats-officedocument.presentationml.presentation |
MS Word document | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
JPEG | image/jpeg |
PNG | image/png |
SVG | image/svg+xml |
JSON | application/vnd.google-apps.script+json |
See the site below https://developers.google.com/drive/api/v2/ref-export-formats
Use the GetList
method to list the contents specified by query and extract the desired data.
Get data from search results
file_list = drive.ListFile({'q':"title='Hello' and mimeType='application/vnd.google-apps.spreadsheet' and trashed = false"}).GetList()
for file in file_list:
#File/Contents you want to extract from the folder
print(file['title'], file['id'])
Property name | type | Description |
---|---|---|
id | string | File ID |
selfLink | string | File URL |
title | string | file name |
mimeType | string | File mimeType |
There are many other items that can be extracted, so see below for details. https://developers.google.com/drive/api/v2/reference/files
Recommended Posts