Tool to generate files on Google Drive from a JSON file
Project description
Drive files generator
Tool to generate folders and documents on Google Drive from a JSON file.
JSON file tree
The JSON file describes the file tree you want to create on Google Drive.
An example will help you understand the structure of it.
{
"my-drive": {
"docs": ["My Google Docs"],
"sheets": 3,
"folders": {
"ZORRO": {
"forms": 1,
"sites": ["Google Site #1", "Google Site #2"],
"folders": {
"my last folder": {}
}
}
}
}
}
-
The outter object must have a single entry. The key is the ID of the folder where you want to create the file tree or the special value
my-drive
if you want it to be the root of your Drive. A shared drive ID can be passed too. -
Inside a folder, you can specify the different Google Drive elements you want to create. Either documents from the the list, or the special "folders" entry if you want to create subfolders.
-
A folder is identified by its name (= JSON key). Exception for the root element which is
my-drive
or an existing Google Drive ID. -
To specify which Google Docs, Sheets, Sites (and so on) you want to create, you can either tell how many you want and they will have a timestamp for name (
"docs": 3
will create 3 Google Docs - not possible for folders at the moment) or you can specify a list of names ("docs": ["one", "two", "three"]
). -
If you want to create an empty folder, just add an entry with an empty dict (
"my last folder": {}
)
Types of Google Drive files
Here are the types of files you can create. The left-hand side is what you put in your JSON file, the right-hand side is the corresponding mime type in Google Drive.
{
"docs": "application/vnd.google-apps.document",
"drawings": "application/vnd.google-apps.drawing",
"forms": "application/vnd.google-apps.form",
"slides": "application/vnd.google-apps.presentation",
"scripts": "application/vnd.google-apps.script",
"sites": "application/vnd.google-apps.site",
"sheets": "application/vnd.google-apps.spreadsheet",
}
Credentials
At the moment, the script is not packaged nor published as an app. So you have to create your own GCP Oauth
client ID for installed app (this procedure).
Download the JSON file and pass it to the command line.
The script will ask for permission to write in Google Drive. After the scope authorization process is done, it
will save your refresh token + access token into a file if (and only if) you have passed --store-creds
with a file
path to the command line.
Otherwise it will ask your permission at each run.
Run the script
usage: drive_files_gen.py [-h] [--store-creds STORE_CREDS] client_id_file json_file
Generate Google Drive folders and documents from a JSON file.
positional arguments:
client_id_file the client id JSON file, downloaded from GCP
json_file the Drive file tree, in JSON format
optional arguments:
-h, --help show this help message and exit
--store-creds STORE_CREDS
if you want to store your creds (refresh + access token) on your filesystem, give it a file path
Test the script
I've made a bit of unit testing with pytest
. It requires to have both a JSON Oauth client file and
a valid JSON credentials file (with the refresh token + access token).
You will see in test_main.py
that I reference a test_config
package, which is where I store the path to JSON files.
To test it, simply run pytest
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
Hashes for drive_files_gen-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1f8f2d60e4bb01241be3ebc886547da31beb2c24a185d5991ff1096445ffa43 |
|
MD5 | 81c957d2244c26c2b37601ab6052bc7d |
|
BLAKE2b-256 | 9bf4f010535b2cd749add849b4c1f9dc58c8b1818551ac78c0d2f33f407c82a3 |