LoginSignup
2
4

More than 1 year has passed since last update.

Google ColaboratoryでGoogleドライブを検索する方法

Last updated at Posted at 2020-09-22

pydriveを使用するための準備

認証コード作成
from google.colab import auth
auth.authenticate_user()
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)

認証作業なしで実行する場合は下記を参考にしてください。
https://qiita.com/plumfield56/items/3d9e234366bcaea794ac

検索方法

上記の状態まで準備できたら、下記で検索を実行します。

drive.ListFile({'q':"{queryの内容を記載}"})

検索例

例えば、タイトルがHellomimeTypeがスプレッドシートゴミ箱以外 を、
検索する場合は下記のように記載します。

drive.ListFile({'q':"title='Hello' and mimeType='application/vnd.google-apps.spreadsheet' and trashed = false"})

その他、検索例は下記サイトを参照
https://developers.google.com/drive/api/v2/search-files#query_string_examples

queryで細かい指定をするためには以下を使用していきます。

queryの指定方法

用語 使用可能な比較演算子 使用方法
title contains, =, != タイトルの検索
シングルクォーテーションを使用する場合はバックスペースでエスケープさせる
fullText contains タイトル、説明、内容、インデックスの検索
mimeType contains, =, != MIME typeの検索
modifiedDate <=, <, =, !=, >, >= 最終更新日の検索
lastViewedByMeDate <=, <, =, !=, >, >= 最後に閲覧した日付で検索
trashed =, != ゴミ箱内まで検索するかの指定(true,falseで指定)
starred =, != お気に入りを付けているものを検索するかの指定(true,falseで指定)
parents in 親のIDを指定
owners in オーナーを指定
writers in 特定のユーザーが編集者になっているかを指定
readers in 特定のユーザーが閲覧者になっているかを指定

下記サイトを参照
https://developers.google.com/drive/api/v2/ref-search-terms

GoogleサービスのMIME Types一覧

サービス名 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

下記サイトを参照
https://developers.google.com/drive/api/v2/mime-types

Googleサービス以外のMIME Types一覧

ファイル形式 MIME Types
PDF 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

下記サイトを参照
https://developers.google.com/drive/api/v2/ref-export-formats
https://developers.google.com/drive/api/v2/mime-types

検索結果からデータ取得

queryで指定した内容を GetList メソッドでリストにして、欲しいデータを抽出します。

検索結果からデータ取得
file_list = drive.ListFile({'q':"title='Hello' and mimeType='application/vnd.google-apps.spreadsheet' and trashed = false"}).GetList()

for file in file_list:
  # ファイル/フォルダから抽出したい内容
  print(file['title'], file['id'])

抽出可能な項目一覧

プロパティ名 タイプ 説明
id string ファイルID
selfLink string ファイルのURL
title string ファイル名
mimeType string ファイルのmimeType

その他、抽出可能な項目は多数あるので、詳細は下記参照
https://developers.google.com/drive/api/v2/reference/files

ファイル、フォルダの作成

スプレッドシート作成の場合
title = 'title'
folder_id = 'id'

f = drive.CreateFile({'title': title,
                     'mimeType': 'application/vnd.google-apps.spreadsheet',
                     'parents': [{'id': folder_id}]})
f.Upload()

下記のように別で記載しても同じように動きます。

スプレッドシート作成の場合
title = 'title'
folder_id = 'id'

f = drive.CreateFile({'title': title,
                     'mimeType': 'application/vnd.google-apps.spreadsheet'})
f['parents'] = [{'id': folder_id}]
f.Upload()

ファイルのコピー

公式サイト
https://developers.google.com/drive/api/v2/reference/files/copy#examples

公式サイトの情報は詳細がかかれていなくて参考にしづらいです。

調べた中だとこちらが一番詳細に書かれていました。
https://stackoverflow.com/questions/65450228/google-drive-api-copy-a-file-in-team-drive

dst_parent_id = '1WgZMCQfUX4OizIJ_TYQUBWNs6vNai8wc'
copy_file_id = '1TYvRg6xvHPiZqIOPKZz8XiyriM9sL_yfdQ-sPVh_LbU' # BBさん
new_title

clone = drive.auth.service.files().copy(fileId=copy_file_id,
                           body={'parents': [{'kind': 'drive#fileLink',
                                 'id': dst_parent_id }], 'title': new_title}).execute()
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4