Google Drive client
Project description
Drive
Google Drive client.
Install
python -m pip install drive
With Poetry:
poetry add drive
You also need to have libmagic
installed to get automatic detection of uploaded files’ MIME types. If you don’t have
it, you must provide the original_mime_type
keyword argument when you upload a file.
On Linux, if you do have libmagic
but Python can’t see it, see this StackOverflow question.
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 File
s, 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 fileupload_excel_workbook(parent, name, workbook)
: Upload anopenpyxl
workbook in a Google spreadsheet underparent
with the namename
.
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 typeexists()
(bool
)unlink()
(bool
): Remove the file. If it's a directory, all its children are removed as wellrename(new_name)
: Rename the filemove_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 contentcreate_folder(name)
: Create a folder under the current oneget_or_create_folder(name)
: Retrieve a child folder or create it if it doesn’t existget_child(name)
: Return a file under the current directory.parents()
: Return a file's parentsparent()
: Return the first parent of a filedownload_file(path[, mime_type])
: Download the file at a given locationdownload_workbook()
: Download the file as anopenpyxl
workbookjson()
: Parse the file as JSONjsons()
: 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-2023 Baptiste Fontaine
Distributed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drive-0.4.4.tar.gz
.
File metadata
- Download URL: drive-0.4.4.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.7 Linux/5.15.0-1040-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2896354e68c9f253363e82de4fb8ad53041c3b26f3b12e7ff9be6bcbde2c5c2a |
|
MD5 | 882a4d100697c2b2a7d6523629092153 |
|
BLAKE2b-256 | c5a67e0822156abdcca81ff5886b4a56686f3aec5f400e695aa109ca28013797 |
File details
Details for the file drive-0.4.4-py3-none-any.whl
.
File metadata
- Download URL: drive-0.4.4-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.7 Linux/5.15.0-1040-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 837e451558477ea464ffaed5e102cc5cf3986271d845b8dfddef32e032642dc6 |
|
MD5 | 9ac9ae5c1a0ffdd8e05ceb2ff336162a |
|
BLAKE2b-256 | 65856aba556bf0b021efb204c85f87b0543fb14a49756a2ac322588444b489f4 |