Show
A guide to creating spreadsheets and saving data to Google Sheets with Python.Image via Shutterstock under license to Frank AndradeDid you know Google Sheets can be used as a lightweight database? Google Sheets is a cloud-based spreadsheet application that can store data in a structured way just like most database management systems. You can also edit and delete data with a couple of clicks and, on top of that, access your data online. Although Google Sheets has limitations such as the numbers of cells you can use (up to 10 million cells) and API calls you can make per minute, it’s still a good option that you can get for free. In this guide, I’ll show you how to use Google Sheets with Python. We’ll learn how to create a spreadsheet and save data to it using Python, so you can turn Google Sheets into your own database. Table of Contents If you don’t feel like reading, you can watch my video instead! 1. Enable Google Drive and Google Sheet API and download credentialsTo connect Google Sheets with Python, we have to download our credentials in a JSON file. To do so, we have to start a new project. Create a new projectTo create a new project, go to this website and follow the steps below (you need to be already logged into your Google account).
4. Once you create your project, you’ll see the image below Image by authorClick on “Select Project.” You should see now a new page. Enable Google Drive API for the new project, create credentials, and download itAfter selecting the project, go to the left panel and click on “APIs & Services” and then select “Library” as shown below. Image by authorYou should see the page below now. There we have to search Google Drive API and enable it. Image by authorOnce it’s enabled, we have to click on “Create Credentials” on the upper-right corner. After this, we have to select the options below and click on “Next” Image by authorThen we give a name to our service account and click on “Create and Continue” Image by authorNow we have to select the role. In this case, we select “Editor” from the “Project” option and then click on “Done.” Image by authorAfter this, we have to open the left panel again click on “APIs & Services” and now select select “Credentials” At the bottom of the page, you’ll see a “Service Account” section. Here we have to click on our client email (keep in mind that all the files we’ll create in the section will be saved in this client email) Image by authorThen we have to go to the “Keys” section and click on “Create new key” Then click on “Create” and a JSON file with your credentials will be downloaded to your computer. I’ll rename this file as “gs_credentials.json” You have to move this JSON file to the same folder where your Python script is located. Finally, we have to search Google Sheets API and enable it as we previously did for Google Drive API. 2. Turning Google Sheets into a Database with PythonConnect to Google SheetsTo work with Google Sheets in Python, first, we have to install gspread and oauth2client. In addition to that, we’ll use Pandas to read our local data and update it to Google Sheets. pip install gspread Now let’s import the libraries and connect to Google Sheets. In the code above, we enable access to
specific links in the Finally, we create a Create a blank spreadsheetLet’s create a new spreadsheet named “NewDatabase” To do so, we have to use the sheet = client.create("NewDatabase") Note that this new sheet is only visible to the service account we created before. To access this sheet with our own Google account, we must share it with our email Sharing a spreadsheetLet’s share this sheet.share('your_email_goes_here', perm_type='user', role='writer') After running the code above, the sheet will be in the “Shared with me” section in your Google Drive. Open the spreadsheet and upload dataNow it’s time to open the sheet and upload data to it. To do so, we use
the Then we read any CSV file we have with Pandas. In this case, I’ll use a CSV file that you can find Google Drive or my Github, but feel free to use any CSV file you want. Finally, we upload the data to our sheet using the # Open the spreadsheet That’s it! Now your sheet should have the same data there’s in the CSV file. You can find all the code written in this guide on my Github. Bonus: Open Any Google Sheet File with PythonNow it’s time to open any Google Sheet file (even those that were not created by our client email). To do so, open any Google Sheet file, go to the upper-right corner and click on “Share.” Image by authorA new window will pop up. Insert your client email that we got before (which is also in the JSON file we downloaded) and click on “Send” Image by authorGreat! Now that we have permission, you can open this using the same code we wrote before. How does Google Sheets work with Python?A Google account.. Step 1: Install the Google client library. To install the Google client library for Python, run the following command: pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib. ... . Step 2: Configure the sample. To configure the sample: ... . Step 3: Run the sample. To run the sample:. Can Google Sheets be automated?Sheet Automation is a powerful, easy-to-use extension that takes Google Sheets to next level. Create your mini workflow with the intuitive UI and automate everything without writing script.
How do I write in Google Sheets using Python?Reading and writing to Google Spreadsheets using Python. Head over to the Google API Console.. Create a new project by selecting My Project -> + button.. Search for 'Google Drive API', enable it.. Head over to 'Credentials' (sidebar), click 'Create Credentials' -> 'Service Account Key'. Can Python update Google sheet?To work with Google Sheets in Python, first, we have to install gspread and oauth2client. In addition to that, we'll use Pandas to read our local data and update it to Google Sheets. Now let's import the libraries and connect to Google Sheets.
|