This script allows you to color-code your Gmail labels programmatically using the Gmail API. It ensures good contrast between background and text colors and cycles through available colors without repetition until all options have been used.
- Automatically colors Gmail labels with user-defined colors.
- Ensures that background colors are only repeated after all have been used.
- Offers flexibility to use either dark backgrounds with light text or light backgrounds with dark text.
- Supports nested labels, applying the same color to parent and nested labels.
- Python 3.x installed on your system.
- Access to OAuth 2.0 Playground
-
Clone this gist or download the files directly.
-
Install Required Python Packages:
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client requests
-
Download the
gmail-label-colors.json
file. This file defines the available background and text colors to be used by the script. -
Obtain an Access Token using OAuth 2.0 Playground: To interact with Gmail programmatically, follow these steps to get an
ACCESS_TOKEN
using OAuth 2.0 Playground:- Visit OAuth 2.0 Playground
- In the left panel, scroll down to "Gmail API v1" and select https://www.googleapis.com/auth/gmail.labels.
- Click on the "Authorize APIs" button.
- Sign in with your Google account and grant the necessary permissions.
- After authorization, you’ll be redirected back to the OAuth 2.0 Playground.
- Click on "Exchange authorization code for tokens" to get your Access Token.
- Copy the Access Token displayed.
Note: The access token is temporary and will expire after one hour. You can repeat the above steps to generate a new token when needed.
-
Run the Script With the prerequisites in place, you can run the script as follows:
python gmail-label-colors.py
-
Customize the Script Toggle Dark or Light Backgrounds: Modify the
use_dark_background
variable in the script to choose whether you want dark backgrounds with light text or light backgrounds with dark text.
This project is open-source and available under the MIT License.