Skip to main content

Google Drive client

Project description

Drive

Google Drive client.

Install

python -m pip install drive

With Poetry:

poetry add drive

Usage

The API exposes a client as drive.client.Client that manipulates instances of drive.files.File. A File represent a Google Drive file. Note that both regular files and directories are represented as Files, and a file can have multiple parent directories. You can check if a File is a directory using the is_directory attribute.

Note: "Folder" is just a synonym for "Directory".

Authentication

By default, the client reads your service account key JSON file at the location given by the environment variable GOOGLE_APPLICATION_CREDENTIALS. You can override this behavior by passing it directly:

client = Client("/path/to/your/service-account-key.json")

See Google’s documentation on how to create a service account key.

Client

High-level Client methods:

  • get_file(file_id) (File)
  • get_file_by_name(name) (File)
  • files_shared_with_me() (File list)
  • get_shared_directory(name) (File)
  • root() (File)
  • upload_file(parent, path[, name]): Upload a file
  • upload_excel_workbook(parent, name, workbook): Upload an openpyxl workbook in a Google spreadsheet under parent with the name name.

The client also exposes low-level methods that work on file ids.

File

  • id (str, attribute)
  • name (str, attribute)
  • is_directory (bool, attribute)
  • human_type (str, attribute): Human-readable file type
  • exists() (bool)
  • unlink() (bool): Remove the file. If it's a directory, all its children are removed as well
  • rename(new_name): Rename the file
  • move_in(new_parent[, new_name]): Move a file under another directory. It can also rename the file at the same time.
  • list(): List a directory’s content
  • create_folder(name): Create a folder under the current one
  • get_or_create_folder(name): Retrieve a child folder or create it if it doesn’t exist
  • get_child(name): Return a file under the current directory.
  • parents(): Return a file's parents
  • parent(): Return the first parent of a file
  • download_file(path[, mime_type]): Download the file at a given location
  • download_workbook(): Download the file as an openpyxl workbook
  • json(): Parse the file as JSON
  • jsons(): Parse the file as JSONS (one JSON per line) and returns a generator

Methods that operate on directories (e.g. list()) generally have no effect if the File instance is a regular file.

Examples

from drive.client import Client

# Uses credentials from the path in the environment variable
# GOOGLE_APPLICATION_CREDENTIALS.
cl = Client()

# Get the root directory
d = cl.root()
print(d.is_directory) # True
print(d.name) # e.g. "My Drive"

# Get a directory's content
for f in d.list():
    print(f.name)

# Get a shared directory
d = cl.get_shared_directory("My Shared Dir")

Spreadsheets

from drive.client import Client
from openpyxl import Workbook

cl = Client()

# Download
f = cl.get_file_by_name("my_sheet")
workbook = f.download_workbook()  # openpyxl workbook
# save your download:
workbook.save("myfile.xlsx")

# Upload
workbook = Workbook()
d = cl.get_shared_directory("My Shared Directory")
cl.upload_excel_workbook(d, "my_other_sheet", workbook)

Drawings

from drive.client import Client

cl = Client()
# download a Drawing in a png image
cl.download_file("11AASomeFileId", "localfile.png", "image/png")

License

Copyright © 2016-2022 Baptiste Fontaine

Distributed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drive-0.4.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

drive-0.4.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file drive-0.4.0.tar.gz.

File metadata

  • Download URL: drive-0.4.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.7 Linux/5.15.0-1020-azure

File hashes

Hashes for drive-0.4.0.tar.gz
Algorithm Hash digest
SHA256 bd387dfa800fa2ecf792740ad0181f3e8972aaa720b3d220554b820c518ccc52
MD5 e0ae74d39043a93f94dd7e78b10efb78
BLAKE2b-256 c5ba19571d78f7d5894576d7fe37845f7bdc609797de12890745366eb388b6b6

See more details on using hashes here.

File details

Details for the file drive-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: drive-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.7 Linux/5.15.0-1020-azure

File hashes

Hashes for drive-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ed9309e74bcc6fd90fa5f8ed445b9e537c915b49fa906cea918eff8e3f94caa
MD5 ca476ae56f6f0544d76018b37f90ebb1
BLAKE2b-256 a99261f70c85dc47d3fe794ac333c06eaf8c271fdb77bc7d966aada16384e612

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page