Skip to main content

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": {}
        }   
      }
    }
  }
}
  1. 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.

  2. 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.

  3. A folder is identified by its name (= JSON key). Exception for the root element which is my-drive or an existing Google Drive ID.

  4. 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"]).

  5. 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

Release files for drive-files-gen 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drive-files-gen 0.0.4
File Size Uploaded
drive-files-gen-0.0.4.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drive-files-gen 0.0.4
File Interpreter ABI Platform
drive_files_gen-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / drive-files-gen-0.0.4.tar.gz

Download URL drive-files-gen-0.0.4.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
227f5a7d51966f20f080ca8f1dddfd4b63af74b65675c3f00dbba810c06d982c
BLAKE2b-256 checksum
How to use checksums
ea644cd8e2a5e6b6130339f7bcf1f7d65bdc9e8371912d9dd7a52cdf31e03b50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / drive_files_gen-0.0.4-py3-none-any.whl

Download URL drive_files_gen-0.0.4-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1f8f2d60e4bb01241be3ebc886547da31beb2c24a185d5991ff1096445ffa43
BLAKE2b-256 checksum
How to use checksums
9bf4f010535b2cd749add849b4c1f9dc58c8b1818551ac78c0d2f33f407c82a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page