Parsing tables from Gmail messages

I get a lot of emails from a certain financial institution and I decided I wanted to parse the information they send me as best I could. This is actually pretty easy when you’ve got a Gmail account, because you can hook into your email in a non-clunky way.

I use Anaconda to create Python projects on Windows, and it informs you to get the Google APIs for Python by running:

conda install -c mutirri google-api-python-client

But that didn’t do what I expected, so I launched the Anaconda Prompt and did as Google said and ran the typical:

pip install --upgrade google-api-python-client oauth2client

When I ran that, I got an error saying that

twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.

So, I ran…

pip install PyHamcrest

You will also have to install argparse if you do not already have it. There may be other dependencies to download if not using the default installation of Anaconda.

To securely access your own account from the API, you must direct your script to your credentials. This is easily done by creating a project on your account and downloading the credentials as a .json file. From this link:

  1. Sign into your Google account if you are not already
  2. Click “Enable the Gmail API”
  3. Create a project by selecting “Create a new project” from the “Select or create project” dropdown in the dialog2018-11-18 14_01_33-Python Quickstart  _  Gmail API  _  Google Developers - Opera.png
  4. Enter a project name and click next2018-11-18 13_55_56-Python Quickstart  _  Gmail API  _  Google Developers - Opera.png
  5. Click Download Client Configuration and put the credentials.json file in the same directory that will hold your future Python script.2018-11-18 13_57_08-Python Quickstart  _  Gmail API  _  Google Developers - Opera.png

Don’t give these credentials away. Do not commit it to a public Git repository. To be safe, never commit it, and add it to your .gitignore file.

If you think some credentials may have been compromised, you can disable it via the Google developers console. You can also see traffic and other information from here.

Armed with your credentials, simply create the script quickstart.py, and copy and paste the quickstart example into it. This example fetches the labels from your Gmail and prints them out.

The first time you run it, your browser will launch and ask you to sign into your Google account, then to approve “Quickstart” to access your account.

The browser should say:

The authentication flow has completed.

Back in your Python project, it should print out the labels from your Gmail account.

That’s it! This and any future progress on this project can be found on Github.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s