How to Download a Specific Sheet by Name from a Google Spreadsheet as a CSV File | by billydharmawan | Towards Data Science

In the past two tutorials on Google Drive API with Python, we have covered how to obtain credentials here and search for a specific file in Google Drive by its name here.
In this tutorial, we are going to learn how to download a specific Sheet by name from a Google Spreadsheet into a csv file. A use case for this: you need to generate a report, which data are stored in a Google Spreadsheet that has many Sheets, but you only need one or two of them. So, instead of downloading the whole Google Spreadsheet, you can handpick the Sheets that you need.
Why by its name? Because I’ve learnt from my work experience that people tend to stick to the naming and usually refer to it by its name. For instance, one might ask his or colleague like this, “Can you extract the data from the Revenue sheet, please?.
Anyways, you know what I’m talking about. 💩Let’s just get into it, alright?
TLDR; download the project repo from Github.
Our current Python script has the capability to search for a specific file by its name. Let’s modify the script so then it is able to reference a specific Sheet in a Google Spreadsheet and download it as a csv file.
At the end of the tutorial, we will end up with a Python script that is executable from command line and takes two input arguments:
Google Spreadsheet’s name
Sheet name to download as csv
To follow along, please check out this commit of the repo.
The first thing we need to do is to enable the Google Sheets API. Now, open your browser and go to https://console.developers.google.com/apis/dashboard. Then, make sure you are on the right project and if not, select the right project from the drop down list.
Next, go to the Library tab and type “Sheets” on the search bar.
Go ahead and click on it. Once you land on the Google Sheets API page, click on the ENABLE button to enable this API.
After that, if you just go to the Credentials tab, you will see that it says our credentials we set up earlier for Google Drive API is compatible with the Sheets API. This means we can use the credentials that we already have and set up for our script. 🙂
To be able to use the Sheets API, we need to initialise a Sheets service instance as follows.
sheets = discovery.build('sheets', 'v4', credentials=credentials)
For now, let’s add the above code to our existing connect_to_google_drive.py file (we’ll refactor this later).
Now that we narrow down the functionality of our script, we can add a filter to our existing retrieve_all_files to only look for Google Spreadsheet type of files. To do this, we just need to add one parameter q as follows.
# define a function to retrieve all filesdef retrieve_all_files(api_service, filename_to_search):    results = []    page_token = None    while True:        try:            param = {'q': 'mimeType="application/vnd.google-apps.spreadsheet"'}            if page_token:                param['pageToken'] = page_token            files = api_service.files().list(**param).execute()    ...    ...    ...
By adding mimeType="application/vnd.google-apps.spreadsheet", it tells the Google Drive API’s list() method to only return the result(s) if the file’s mime type is a Google Spreadsheet.
Finally, let’s write the function that will use the Sheets’ API’s values() method to get cell values from a specific sheet snd write them to a csv file.
# define a function to export sheet to csvdef download_sheet_to_csv(spreadsheet_id, sheet_name):    result = sheets.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=sheet_name).execute()    output_file = f'{sheet_name}.csv'    with open(output_file, 'w') as f:        writer = csv.writer(f)        writer.writerows(result.get('values'))    f.close()    print(f'Successfully downloaded {sheet_name}.csv')
Let’s call this new function at the end of our script, like so.
# call the functionfilename_to_search = 'Christmas Plan'all_files, search_file = retrieve_all_files(drive, filename_to_search)download_sheet_to_csv(search_file.get('id'), 'Sheet2')
For this example, the content of the Sheet2 is like this.
Cool. Now go ahead and run this script from the terminal. Remember to activate your virtual environment before running the script. 🙂 This is what you will see in the console when the script is finished.
$ python connect_to_google_drive.py{'kind': 'drive#file', 'id': '1GyIz1NqCg6Bkr0Z_3Craem0BAwG0195usRswduCtKab', 'name': 'Christmas Plan', 'mimeType': 'application/vnd.google-apps.spreadsheet'}Successfully downloaded Sheet2.csv
Let’s validate the downloaded file by looking at its content.
$ cat Sheet2.csvNo,Code,Name1,123,Apple2,234,Watermelon3,345,Banana4,456,Blueberry5,567,Dragonfruit6,678,Cherry7,789,Strawberry8,890,Kiwi9,901,Avocado
Alright. It looks like our script works as expected. Happy days 😃
As promised, we will now refactor the code and transform it so that it will take two input arguments from the command line.
To separate things out, we’re going to extract all the functions in connect_to_google_drive.py into individual Python files. Let’s create a folder and name it google_api_functions.
First, let’s extract the lines of code that initialise our drive and sheets instances into a new file and name it get_api_services.py.
Next, we’ll refactor the retrieve_all_files function. Now, the only information we need from this function is the id of the Google Spreadsheet. So, let’s remove the unnecessary lines and finally, extract it to a new Python file named get_spreadsheet_id.py.
The last function to extract out is the download_sheet_to_csv function. Let’s extract it into a new file named download_sheet_to_csv.py.
Now, we will rename our connect_to_google_drive.py file to main.py. This is the Python file that we will call from the command line.
We are going to add a function in this file that will parse arguments passed through the command line, which will be the spreadsheet name and the sheet name.
def parse_args():    parser = argparse.ArgumentParser(description="Function to download a specific sheet from a Google Spreadsheet")    parser.add_argument("--spreadsheet-name", required=True, help="The name of the Google Spreadsheet")    parser.add_argument("--sheet-name", required=True, help="The name of the sheet in spreadsheet to download as csv")    return parser.parse_args()
Finally, let’s write the lines of code that will get executed when the script main.py is called from the command line.
if __name__ == '__main__':    args = parse_args()    spreadsheet_name = args.spreadsheet_name    sheet_name = args.sheet_name    drive, sheets = get_api_services()    spreadsheet_id = get_spreadsheet_id(drive, spreadsheet_name)    download_sheet_to_csv(sheets, spreadsheet_id, sheet_name)
Nice! Our code becomes so much cleaner and easier to follow now. 🙂
With this new main.py script, the way we can call it from the command line is as follows.
$ python main.py --spreadsheet-name "Christmas Plan" --sheet-name Sheet2Successfully downloaded Sheet2.csv
That’s it. 😺
Over the past three tutorials, we have discussed the following:
Set up credentials to use the Google Drive API — link
Search for a file in Google Drive — link
Download a specific sheet as csv
By now, we should all be familiar with Google Drive and Sheets API. There are still a lot of other things that we can do using the API and so, I encourage all of you to explore it further and build some automation script to help with your work or life in general. 🙏