Recently, I had to write a Python program to automatically read emails with a specific label from my Gmail account and process the excel sheet attached in that mail daily at a specific time.
I knew that processing the excel sheet will be an easy task with Pandas but the real problem was getting the excel file, especially the second sheet of it. This took me quite some time and finally I was able to find a workaround.
To read emails from Gmail, there are two ways — using imaplib or using Gmail API.
To use imaplib, just allow it the access (Go to Settings > Forwarding and POP/IMAP > Enable IMAP) and follow this StackOverflow answer.
I wanted to use the official Gmail API because I have never used them before. If you need to make thousands of API calls, then check out the Usage Limits page. In my case, I needed to make a single API call each day so it’s free.
Follow the instructions in the Python Quickstart page to create a Google Clouds Platform project and download the credentials file. Create the skeleton of the Quickstart as mentioned in the documentation.
After that use the code below to read the excel sheet.