{"id":3452,"date":"2024-05-15T15:26:17","date_gmt":"2024-05-15T10:26:17","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3452"},"modified":"2024-05-15T15:26:21","modified_gmt":"2024-05-15T10:26:21","slug":"using-google-sheet-api-with-python","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/05\/15\/using-google-sheet-api-with-python\/","title":{"rendered":"Using Google Sheet API with Python"},"content":{"rendered":"\n<p>Here is the step-by-step guide to connecting Google Sheets to any Python program. In this tutorial, we connect it to Google Collab, but you can connect it to any application with the following the below points. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Requirement:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A Google account A Google Cloud Platform (GCP) project (<a href=\"https:\/\/developers.google.com\/workspace\/guides\/create-project\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/developers.google.com\/workspace\/guides\/create-project<\/a>) <\/li>\n\n\n\n<li>Python 3.x installed (<a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.python.org\/downloads\/<\/a>) <\/li>\n\n\n\n<li>A code editor (e.g., Visual Studio Code, PyCharm,Google Colab)<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"306\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?resize=640%2C306&#038;ssl=1\" alt=\"\" class=\"wp-image-3458\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?resize=1024%2C490&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?resize=300%2C143&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?resize=768%2C367&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?resize=564%2C270&amp;ssl=1 564w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?w=1361&amp;ssl=1 1361w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-4.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Open google consle<\/figcaption><\/figure>\n\n\n\n<p><strong>1. Create a New Google Cloud Platform (GCP) Project<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you don&#8217;t have a GCP project already, you&#8217;ll need to create one. Head over to the GCP Console: <a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/console.cloud.google.com\/\">https:\/\/console.cloud.google.com\/<\/a>.<\/li>\n\n\n\n<li>Click on the &#8220;Select a project&#8221; dropdown in the top navigation bar.<\/li>\n\n\n\n<li>If you don&#8217;t see any projects listed, choose &#8220;Create project.&#8221;<\/li>\n\n\n\n<li>Give your project a unique and descriptive name (e.g., &#8220;Google Sheets API Project&#8221;).<\/li>\n\n\n\n<li>Choose an organization (if applicable) and location for your project&#8217;s resources.<\/li>\n\n\n\n<li>Click &#8220;Create&#8221; to establish your new GCP project.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"319\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?resize=640%2C319&#038;ssl=1\" alt=\"\" class=\"wp-image-3459\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?resize=1024%2C511&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?resize=768%2C383&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?resize=541%2C270&amp;ssl=1 541w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?w=1345&amp;ssl=1 1345w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-5.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Google console library<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"311\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?resize=640%2C311&#038;ssl=1\" alt=\"\" class=\"wp-image-3460\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?resize=1024%2C497&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?resize=300%2C146&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?resize=768%2C373&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?resize=556%2C270&amp;ssl=1 556w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?w=1341&amp;ssl=1 1341w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-6.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Google sheets API<\/figcaption><\/figure>\n\n\n\n<p><strong>2. Enable Google Sheets API<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now that you have a GCP project, you need to enable the specific API you want to use: Google Sheets API.<\/li>\n\n\n\n<li>In the GCP Console navigation bar, go to the &#8220;APIs &amp; Services&#8221; section.<\/li>\n\n\n\n<li>Select &#8220;Library&#8221; from the submenu. This is where you&#8217;ll find all available Google Cloud APIs.<\/li>\n\n\n\n<li>Use the search bar to find &#8220;Google Sheets API.&#8221;<\/li>\n\n\n\n<li>Once you locate it, click on the API name to access its details.<\/li>\n\n\n\n<li>On the API details page, click the &#8220;Enable&#8221; button to activate the Google Sheets API for your project.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"289\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=640%2C289&#038;ssl=1\" alt=\"\" class=\"wp-image-3462\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=1024%2C463&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=300%2C136&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=768%2C348&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=597%2C270&amp;ssl=1 597w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?w=1337&amp;ssl=1 1337w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-8.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">To create service account<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"278\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-9.png?resize=640%2C278&#038;ssl=1\" alt=\"\" class=\"wp-image-3463\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-9.png?resize=1024%2C445&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-9.png?resize=300%2C130&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-9.png?resize=768%2C333&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-9.png?resize=604%2C262&amp;ssl=1 604w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-9.png?w=1315&amp;ssl=1 1315w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Service accounts<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-3464\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?w=1366&amp;ssl=1 1366w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-10.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Creating new service account<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"323\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?resize=640%2C323&#038;ssl=1\" alt=\"\" class=\"wp-image-3466\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?resize=1024%2C516&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?resize=300%2C151&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?resize=768%2C387&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?resize=536%2C270&amp;ssl=1 536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?w=1355&amp;ssl=1 1355w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-12.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Adding new key<\/figcaption><\/figure>\n\n\n\n<p><strong>3. Create a New Service Account<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Service accounts are special Google accounts used by applications to access Google Cloud resources. For interacting with the Sheets API, you&#8217;ll need a service account.<\/li>\n\n\n\n<li>In the GCP Console navigation bar, go to &#8220;IAM &amp; Admin&#8221; and then select &#8220;Service Accounts.&#8221;<\/li>\n\n\n\n<li>Click the &#8220;Create Service Account&#8221; button.<\/li>\n\n\n\n<li>Give your service account a descriptive name (e.g., &#8220;Sheets API Access&#8221;).<\/li>\n\n\n\n<li>Optionally, describe your service account.<\/li>\n\n\n\n<li>Click &#8220;Create&#8221; to establish the new service account.<\/li>\n\n\n\n<li>Click on the key to add a new key<\/li>\n<\/ul>\n\n\n\n<p><strong>4. Download the JSON File<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Once your service account is created, you need to download its credentials in JSON format. These credentials will be used to authenticate your application with Google when accessing the Sheets API.<\/li>\n\n\n\n<li>Click on the newly created service account name in the list.<\/li>\n\n\n\n<li>Go to the &#8220;Keys&#8221; tab.<\/li>\n\n\n\n<li>Click the &#8220;Add Key&#8221; button and select &#8220;Create new key.&#8221;<\/li>\n\n\n\n<li>Choose the JSON format for the key type.<\/li>\n\n\n\n<li>Click &#8220;Create&#8221; to download the JSON file. <\/li>\n<\/ul>\n\n\n\n<p><strong>Program side:<\/strong><\/p>\n\n\n\n<p>Installed the required libraries<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!pip install google-cloud\r\n!pip install gspread<\/code><\/pre>\n\n\n\n<p>Upload the JSON file, which you downloaded from the google API to the working directory<\/p>\n\n\n\n<p>Once you uploaded, write the following program to connect to the google sheet. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import gspread\r\nfrom google.oauth2.service_account import Credentials\r\nimport pandas as pd\r\n\r\n# Authenticate using the uploaded JSON file\r\nscope = &#91;'https:\/\/spreadsheets.google.com\/feeds', 'https:\/\/www.googleapis.com\/auth\/drive']\r\ncreds = Credentials.from_service_account_file('\/content\/your JSON file', scopes=scope)\r\ngc = gspread.authorize(creds)\r\n\r\n# Open the Google Sheet by its URL\r\nsheet_url = 'your-url'\r\nsheet = gc.open_by_url(sheet_url)\r\n\r\n# Select the first worksheet\r\nworksheet = sheet.get_worksheet(0)\r\n\r\n# Read data from the worksheet into a Pandas DataFrame\r\ndata = worksheet.get_all_values()\r\ndf = pd.DataFrame(data&#91;1:], columns=data&#91;0])  # Assuming the first row contains column headers\r\n\r\n# Display the DataFrame\r\nprint(df)<\/code><\/pre>\n\n\n\n<p>In the above program, replace the google sheet URL and json file names according to your project. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"321\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-2.png?resize=640%2C321&#038;ssl=1\" alt=\"\" class=\"wp-image-3455\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-2.png?w=831&amp;ssl=1 831w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-2.png?resize=300%2C151&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-2.png?resize=768%2C385&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/image-2.png?resize=538%2C270&amp;ssl=1 538w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Output of the program<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is the step-by-step guide to connecting Google Sheets to any Python program. In this tutorial, we connect it to Google Collab, but you can connect it to any application with the following the below points. Requirement: 1. Create a New Google Cloud Platform (GCP) Project 2. Enable Google Sheets API 3. Create a New Service Account 4. Download the JSON File Program side: Installed the required libraries Upload the JSON file, which you downloaded from the google API to&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/05\/15\/using-google-sheet-api-with-python\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3468,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[489],"tags":[592,593],"class_list":["post-3452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-driven-applications-using-mongodb-python-and-google-colab","tag-googel-api","tag-google-sheet-api"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/05\/MongoDB-2-jpg.webp?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-TG","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=3452"}],"version-history":[{"count":5,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3452\/revisions"}],"predecessor-version":[{"id":3470,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3452\/revisions\/3470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3468"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}