How to Use Google Drive API in Python with Simple-Drive

It’s a cool weekend in Saigon today. There is so much I want to write about, but life has been too busy. Then I remembered a handy tool that anyone with a little Python knowledge can use: Simple-Drive, a library I wrote on top of the Google Drive API. It helps you not only work with files (create, edit, delete, upload, download, rename, copy) but also manage permissions, comments, and version history.

Simple-Drive features

Introducing Simple-Drive, an easy-to-use Google Drive API library
Introducing Simple-Drive, an easy-to-use Google Drive API library

Files:

  • create: Create a new file in Google Drive.
  • create_shortcut: Create a shortcut to a file or folder in Google Drive.
  • upload: Upload files from a local device to Google Drive.
  • get: Get details about a specific file on Google Drive.
  • move: Move a file to another folder in Google Drive.
  • copy: Make a copy of a file in Google Drive.
  • rename: Rename files on Google Drive.
  • restrict: Restrict access to a file (only the people you specify can open it).
  • list: List files in Google Drive (with optional filters).
  • download: Download files from Google Drive to a local device.
  • export: Export Google Drive files (like Google Docs and Sheets) to other formats (PDF, Word…).
  • empty_trash: Permanently delete all files from Google Drive’s trash.
  • trash: Move the file to the trash.
  • delete: Permanently delete a file from Google Drive.

Permissions:

  • add: Add access (view, edit) to a user or group for a file or folder.
  • transfer_ownership: Transfer ownership of a file or folder to another user.
  • get: Get detailed information about the access permissions of a file or folder.
  • update: Update current access permissions (for example, change from view-only to edit).
  • list: List all access permissions of a file or folder.
  • remove: Remove user or group access from a file or folder.

Comments:

  • create: Create a new comment on a file (e.g., Google Docs, Sheets).
  • get: Get detailed information about a specific comment.
  • update: Update the content of an existing comment.
  • list: List all comments on a file.
  • delete: Delete a comment from the file.

Replies:

  • create: Reply to a comment on a file.
  • get: Get detailed information about a specific reply.
  • update: Update the content of an existing reply.
  • list: List all replies to a comment.
  • delete: Delete a reply from a comment.

Revisions:

  • get: Get details about a previous version of a file.
  • list: List all revisions of a file.
  • delete: Delete a specific version from the file history.

Install the Simple-Drive library

As long as you have Conda or Python installed on your machine, you can install Simple-Drive with the pip command below.

pip install --upgrade simple-drive

Authorize the Google Drive API

First, import the Simple-Drive modules you need.

from simple_drive import Auth, Drive, MimeTypes, Roles

There are two ways to authorize the Google Drive API:

Method 1: Use a Service Account. This is the most common way to authorize Google APIs, and it keeps your scripts running smoothly because a Service Account never expires. “Create once, use forever,” as I like to joke.

# If the service account is a file
auth = Auth.from_service_account_file(file='service_account.json')

# If the service account is a variable
auth = Auth.from_service_account_info(info)

Method 2: Use your own Google account. This method requires manual sign-in, and the authorization expires after 7 days. You need to create a Client Secret in Google Cloud to do this.

auth = Auth.local_web_server(client_secrets_file='client_secrets.json')

Once authorization is done, initialize the Drive object. The verbose parameter controls whether information is printed while you use the Google Drive API. I recommend leaving it True because the output looks really nice, hehe.

drive = Drive(auth, verbose=True)

Simple-Drive basics

Let me use a scenario to make this easier to follow. Suppose you want to create a folder, create a Google Sheets file inside it, share the file with someone, and add a comment to it.

# Create a folder
folder = drive.Files.create(name='My folder', mime_type=MimeTypes.FOLDER, dest_folder_id=None)

# Create a sheet
sheet = drive.Files.create(name='My sheet', mime_type=MimeTypes.SHEETS, dest_folder_id=folder['id'])

# Add a permission
drive.Permissions.add(file_id=sheet['id'], email='her@gmail.com', role=Roles.EDITOR)

# Create a comment
drive.Comments.create(file_id=sheet['id'], content='Hello, this is today data.')

As you can see in the example above, I organized the classes and functions by feature group. I also included the standard values the Google Drive API requires, such as MimeType and Role, so you don’t have to type them in by hand.

Here is another example. Say you want to run an operation on a batch of files, and you need a list of files that match these criteria:

  1. Files created from 2024-01-01 onwards.
  2. The name contains “Backlog data”.
  3. Located in a specific folder.
from simple_drive import SearchTerms
import pandas as pd

term_1 = SearchTerms.createdTime_greater_equal('2024-01-01')
term_2 = SearchTerms.name_contains('Backlog data')
term_3 = SearchTerms.folder_id('YourFolderID')

files = drive.Files.list(term_1, term_2, term_3, fields='*', operator='and')

df = pd.DataFrame(files)

Once you have the file list, just use a for loop to process all the files.

Conclusion

With more than 30 functions, I believe Simple-Drive can cover many of your Google Drive API needs. Combine it with other libraries, such as gspread for the Google Sheets API, and you can do even more with Google’s APIs.

Hieu Tran Ngoc Minh

Hieu (born in 1996) holds a Bachelor's degree in Business Administration from Saigon Technology University. Currently a Data Analyst at Ninja Van, Hieu has extensive experience in Data Analysis and Digital Marketing. This blog is where Hieu shares practical experiences from work and life.

Leave a Comment

Feel free to leave your comment (preferably in English), and we will review and respond as soon as possible. Please use a real email to ensure your comment is approved and to receive notifications when we reply. You can also add an avatar to your email.