Show
Keep using Python for managing online spreadsheet contentPhoto by Emir Dağcı on UnsplashIf you clicked on this article you’ve probably used Python to manage Excel file content. As a well-spread tool, Excel is a very good option when using spreadsheets. Even though Microsoft has launched OneDrive, many people still use Excel locally and Google Drive’s option, Google Sheets, for the Cloud. Either way, the most important benefit when using a Cloud service is to move away from local files. They demand local storage, they’re difficult to share with friends, and version control is a nightmare. In this article, I’m going to walk you through the steps needed in order for you to be able to access your Google Sheets spreadsheets and manage their content with Python. You may be aware that Google itself offers a native option of the Google Sheets scripts editor, Spreadsheet Service, as part of the Google Apps Script. There are some methods and attributes that are very useful indeed. It also allows you to manage your project execution history and other relevant information through Google’s platform. However, as a Python enthusiast, you’re probably aware of how easy this language is. Let’s list the benefits:
One more good piece of news: From now on, you can check the steps needed in order to set the required access. The rest, you can keep as usual. 1. Enabling External Access to Google SheetsIn this section, we’re going to cover the steps needed in order to grant access from an external application to Google Sheets. The main goal here is to assign your Google’s project credentials to both Google Sheets and Google Drive APIs. This is the process:
Let’s take a look at each of them. 1.1. Set up your project on the Google Cloud PlatformAfter logging in to Google Cloud Platform, if this is your very first time on the platform, you’ll see the following page on the Home section: By clicking “CREATE PROJECT”, you set up your project main information: OK, your project has been created. 1.2. Create your project credentialsNow you need to search for “APIs & Services”. Then, click on “CREATE CREDENTIALS” and then on “Service account” option: Next, you need to enter some basic information: After clicking on “CREATE”, under the “Project” option, click on the “Owner” option: At the bottom of the page, click on your Service Account email link: Then, click on the “ADD KEY” option and “Create new key”: Choose the JSON option: Now you’ve downloaded your JSON project’s credentials file. 1.3. Enable both Google Sheets API and Google Drive APIYou need to search for both “Google Sheets API” and “Google Drive API” (the order isn’t important). As an example, take a look at the “Google Sheets API” page. Here, you have to click on the “ENABLE” button (make sure you also do this for the Google Drive API): Both APIs have been enabled. 1.4. Assign your project credentials to both Google Sheets API and Google Drive APIAfter enabling the APIs, click on “CREATE CREDENTIALS” on the top right page of both of them (again, I printed only the Google Sheets API, below): You’re going to be redirected to the following two screens (for each of the APIs): After selecting the options shown, the Google Cloud Platform will identify your already-created project’s credentials and use it for the APIs: Click “DONE” and now both APIs have been properly identified. 2. Python ScriptSo far, you’ve enabled for external applications to connect to Google Sheets through both Google’s APIs. Now, let’s take a look at what you need to consider in your Python script:
2.1. Requirements and dependenciesYou might think this is the most boring part of the entire flow. You’re wrong. Let’s be straightforward here. You need to fulfill only one requirement, which is to import two libraries: What about the dependencies? Remember the JSON file you have downloaded when creating your project’s credentials? This is one of the dependencies. It mainly covers your private key, which is needed for the authentication process. The other one is just to share access to your Google Sheets spreadsheet with your Service Account email, as you would do with a friend, for instance. You can consult it either on your Credentials session within Google Cloud Platform or on the JSON file (“client_email”), mentioned in the last paragraph. 2.2. Authentication and authorizationSo, if you have the JSON file in the same directory you’re going to execute your .py file, you can consider the following: If, for some reason, you prefer to save your JSON file in a different directory, just make sure you indicate the full path towards it. That’s all for the authentication and authorization session. 2.3. Application scriptWhat I’m calling “Application script” is what you do with your data on Google Sheets. Here’s a simple example, for us to check if you’re able to both import data Google Sheets and read data from it: Checking: Now you’re all set to keep using Python when managing online content on Google Sheets. Does Google Sheets work with Python?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.
Can you edit Google Sheets with Python?For example, if you want to store stock market data from Google into this spreadsheet, then you can write a python script to fetch the data and update the sheet.
How do I read a Google Sheet in Pandas?Here's a quick guide to how it's done.. Install the packages. First, open a Jupyter notebook and install the GSpread Python package by entering ! ... . Authenticate with Google Sheets. ... . Open the Google Sheet with Python. ... . Select the specific worksheet. ... . Bring it all together.. How do you access Google Sheets?You can open Sheets in any of the following ways:. Any web browser—Go to sheets.google.com.. Google Drive—Click New. ... . Most Google pages—In the upper-right corner, click the App Launcher. ... . Android devices—Install and open the Android app.. Apple iOS devices—Install and open the iOS app.. |