1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "name" : " Snippets: Sheets" ,
7
+ "provenance" : [],
8
+ "collapsed_sections" : [],
9
+ "toc_visible" : true ,
10
+ "include_colab_link" : true
11
+ },
12
+ "kernelspec" : {
13
+ "display_name" : " Python 3" ,
14
+ "name" : " python3"
15
+ }
16
+ },
17
+ "cells" : [
18
+ {
19
+ "cell_type" : " markdown" ,
20
+ "metadata" : {
21
+ "id" : " view-in-github" ,
22
+ "colab_type" : " text"
23
+ },
24
+ "source" : [
25
+ " <a href=\" https://colab.research.google.com/github/gulabpatel/Python_Tutorials/blob/master/Colab_Special/Snippets_Sheets.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type" : " markdown" ,
30
+ "metadata" : {
31
+ "id" : " sOm9PFrT8mGG"
32
+ },
33
+ "source" : [
34
+ " # Saving data to Google Sheets\n " ,
35
+ " \n " ,
36
+ " \n " ,
37
+ " This snippet uses the open-source [gspread](https://github.com/burnash/gspread) library for interacting with Sheets."
38
+ ]
39
+ },
40
+ {
41
+ "cell_type" : " code" ,
42
+ "metadata" : {
43
+ "colab" : {
44
+ "base_uri" : " https://localhost:8080/"
45
+ },
46
+ "id" : " 6d0xJz3VzLOo" ,
47
+ "outputId" : " 4a868399-3282-4e6b-c585-e0024b67573a"
48
+ },
49
+ "source" : [
50
+ " from google.colab import auth\n " ,
51
+ " auth.authenticate_user()\n " ,
52
+ " \n " ,
53
+ " import gspread\n " ,
54
+ " from oauth2client.client import GoogleCredentials\n " ,
55
+ " \n " ,
56
+ " gc = gspread.authorize(GoogleCredentials.get_application_default())\n " ,
57
+ " \n " ,
58
+ " sh = gc.create('A new spreadsheet')\n " ,
59
+ " \n " ,
60
+ " # Open our new sheet and add some data.\n " ,
61
+ " worksheet = gc.open('A new spreadsheet').sheet1\n " ,
62
+ " \n " ,
63
+ " cell_list = worksheet.range('A1:C2')\n " ,
64
+ " \n " ,
65
+ " import random\n " ,
66
+ " for cell in cell_list:\n " ,
67
+ " cell.value = random.randint(1, 10)\n " ,
68
+ " \n " ,
69
+ " worksheet.update_cells(cell_list)\n " ,
70
+ " # Go to https://sheets.google.com to see your new spreadsheet."
71
+ ],
72
+ "execution_count" : 1 ,
73
+ "outputs" : [
74
+ {
75
+ "output_type" : " execute_result" ,
76
+ "data" : {
77
+ "text/plain" : [
78
+ " {'spreadsheetId': '1gRQTOA5KGBz-wGKwRn4ARn8zgx2CbBALlhgplIb2qdI',\n " ,
79
+ " 'updatedCells': 6,\n " ,
80
+ " 'updatedColumns': 3,\n " ,
81
+ " 'updatedRange': 'Sheet1!A1:C2',\n " ,
82
+ " 'updatedRows': 2}"
83
+ ]
84
+ },
85
+ "metadata" : {
86
+ "tags" : []
87
+ },
88
+ "execution_count" : 1
89
+ }
90
+ ]
91
+ },
92
+ {
93
+ "cell_type" : " markdown" ,
94
+ "metadata" : {
95
+ "id" : " k9q0pp33dckN"
96
+ },
97
+ "source" : [
98
+ " # Importing data from Google Sheets"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type" : " code" ,
103
+ "metadata" : {
104
+ "colab" : {
105
+ "base_uri" : " https://localhost:8080/" ,
106
+ "height" : 127
107
+ },
108
+ "id" : " JiJVCmu3dhFa" ,
109
+ "outputId" : " b014aeb2-154d-45cd-f74a-8b470a428c26"
110
+ },
111
+ "source" : [
112
+ " from google.colab import auth\n " ,
113
+ " auth.authenticate_user()\n " ,
114
+ " \n " ,
115
+ " import gspread\n " ,
116
+ " from oauth2client.client import GoogleCredentials\n " ,
117
+ " \n " ,
118
+ " gc = gspread.authorize(GoogleCredentials.get_application_default())\n " ,
119
+ " \n " ,
120
+ " worksheet = gc.open('A new spreadsheet').sheet1\n " ,
121
+ " \n " ,
122
+ " # get_all_values gives a list of rows.\n " ,
123
+ " rows = worksheet.get_all_values()\n " ,
124
+ " print(rows)\n " ,
125
+ " \n " ,
126
+ " # Convert to a DataFrame and render.\n " ,
127
+ " import pandas as pd\n " ,
128
+ " pd.DataFrame.from_records(rows)"
129
+ ],
130
+ "execution_count" : 3 ,
131
+ "outputs" : [
132
+ {
133
+ "output_type" : " stream" ,
134
+ "text" : [
135
+ " [['8', '8', '7'], ['4', '3', '10']]\n "
136
+ ],
137
+ "name" : " stdout"
138
+ },
139
+ {
140
+ "output_type" : " execute_result" ,
141
+ "data" : {
142
+ "text/html" : [
143
+ " <div>\n " ,
144
+ " <style scoped>\n " ,
145
+ " .dataframe tbody tr th:only-of-type {\n " ,
146
+ " vertical-align: middle;\n " ,
147
+ " }\n " ,
148
+ " \n " ,
149
+ " .dataframe tbody tr th {\n " ,
150
+ " vertical-align: top;\n " ,
151
+ " }\n " ,
152
+ " \n " ,
153
+ " .dataframe thead th {\n " ,
154
+ " text-align: right;\n " ,
155
+ " }\n " ,
156
+ " </style>\n " ,
157
+ " <table border=\" 1\" class=\" dataframe\" >\n " ,
158
+ " <thead>\n " ,
159
+ " <tr style=\" text-align: right;\" >\n " ,
160
+ " <th></th>\n " ,
161
+ " <th>0</th>\n " ,
162
+ " <th>1</th>\n " ,
163
+ " <th>2</th>\n " ,
164
+ " </tr>\n " ,
165
+ " </thead>\n " ,
166
+ " <tbody>\n " ,
167
+ " <tr>\n " ,
168
+ " <th>0</th>\n " ,
169
+ " <td>8</td>\n " ,
170
+ " <td>8</td>\n " ,
171
+ " <td>7</td>\n " ,
172
+ " </tr>\n " ,
173
+ " <tr>\n " ,
174
+ " <th>1</th>\n " ,
175
+ " <td>4</td>\n " ,
176
+ " <td>3</td>\n " ,
177
+ " <td>10</td>\n " ,
178
+ " </tr>\n " ,
179
+ " </tbody>\n " ,
180
+ " </table>\n " ,
181
+ " </div>"
182
+ ],
183
+ "text/plain" : [
184
+ " 0 1 2\n " ,
185
+ " 0 8 8 7\n " ,
186
+ " 1 4 3 10"
187
+ ]
188
+ },
189
+ "metadata" : {
190
+ "tags" : []
191
+ },
192
+ "execution_count" : 3
193
+ }
194
+ ]
195
+ }
196
+ ]
197
+ }
0 commit comments