Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 95893da

Browse files
Merge pull request #609 from priya-mane/Research_paper_latex_parser
Added Google-spreadsheet-share-and-retrieve script
2 parents dd6c32e + f1a5bfa commit 95893da

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Google Spreadsheet share and retrieve.
2+
3+
This script helps the user to upload a local csv file to google drive and share it with the user specified in the command.
4+
5+
The script also helps to retrieve the spreadsheet form the drive and store it locally in json format.
6+
7+
***
8+
9+
### Prerequisites
10+
```
11+
pip install -r requirements.txt
12+
```
13+
14+
***
15+
16+
### How to run the script
17+
18+
1. #### Create Google Sheets API credentials.
19+
20+
a. Go to [Google Developer's Console](https://console.developers.google.com/apis/dashboard?project=trans-crawler-261018).
21+
22+
b. Go to 'Library' from the left navigation panel.
23+
24+
c. In the search bar type 'Google Sheets API'.
25+
26+
d. Enable the API if not enabled or click on the manage button.
27+
28+
e. Similarly enable the 'Google Drive API'.
29+
30+
f. On the API page, goto 'Credentials' from the Navigation Panel.
31+
32+
g. Click on 'Create Credentials', select the service account option and name the service account.
33+
34+
h. Click on 'Add key' and 'Create New'.
35+
36+
![Google-sheets-api](outputs/google-sheets-credentials.JPG)
37+
38+
i. File download will begin, rename this file as 'credentials.json' and place in the same folder as the scripts.
39+
40+
2. #### Run the Scripts.
41+
42+
* Share a spreadsheet
43+
44+
```
45+
python create_sheet.py -mail <mail_id> -csv <csv_file_name> -s <spreadsheet_name>
46+
```
47+
48+
* Retrieve the spreadsheet and store locally in json format
49+
50+
```
51+
python get_sheet.py -j <json_file_name> -s <spreadsheet_name>
52+
```
53+
54+
***
55+
56+
### Screenshot showing the sample use of the script
57+
58+
1. Creating a google spreadsheet and sharing it with the receiver.
59+
60+
```
61+
python create_sheet.py -mail <receiver-mail_id> -csv sample.csv -s my_sample_spreadsheet
62+
```
63+
64+
The receiver will receive a mail like this.
65+
66+
![successfully-shared](outputs/gmail-noti.JPG)
67+
68+
2. Retrieving a created spreadhsheet.
69+
70+
```
71+
python get_sheet.py -j sample.json -s my_sample_spreadsheet
72+
```
73+
74+
Check the sample.json for observing the output file generated.
75+
76+
***
77+
### Caveats
78+
79+
* Since this is a free access version for the Google Spreadsheet API, keep the size of the csv file low.
80+
81+
* If you get an error such as 'resource limit exhausted', simply create a new key as mention in the step 1.h in the 'How to run the script section'. and replace your old credentials.json file with this new one.
82+
83+
***
84+
85+
## Author Name
86+
Priya Mane
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import gspread
2+
from oauth2client.service_account import ServiceAccountCredentials
3+
import pandas as pd
4+
import argparse
5+
6+
7+
def auth_user():
8+
# Define a authorized user with the credentials created.
9+
scope = ['https://www.googleapis.com/auth/spreadsheets',
10+
'https://www.googleapis.com/auth/drive']
11+
12+
credentials = ServiceAccountCredentials.from_json_keyfile_name(
13+
'credentials.json', scope)
14+
15+
client = gspread.authorize(credentials)
16+
return client
17+
18+
19+
def create_and_share_sheet(user_mail, spreadsheet_name, csv_file):
20+
client = auth_user()
21+
sh = client.create(spreadsheet_name)
22+
worksheet = sh.worksheet("Sheet1")
23+
24+
# Read from the csv file mentioned in the command.
25+
df = pd.read_csv(csv_file)
26+
col = df.columns
27+
# Define the column headings based on the our csv file.
28+
end = ord('A') + len(col) - 1
29+
cell_range = 'A1:' + chr(end) + '1'
30+
31+
# Define cells
32+
cell_list = worksheet.range(cell_range)
33+
i = 0
34+
for cell in cell_list:
35+
cell.value = col[i]
36+
i += 1
37+
38+
# Write these column headings to the worksheet
39+
worksheet.update_cells(cell_list)
40+
41+
# Convert rest of the dataframe to numpy object. (Use pandas version 1.0.3 strictly for this to work!)
42+
df = df.to_numpy().tolist()
43+
44+
# Write data from numpy object to the worksheet
45+
for i in range(2, len(df) + 2):
46+
pos = 'A' + str(i) + ':' + chr(end) + str(i)
47+
cell_list = worksheet.range(pos)
48+
val = df[i-2]
49+
j = 0
50+
for cell in cell_list:
51+
cell.value = val[j]
52+
j += 1
53+
worksheet.update_cells(cell_list)
54+
55+
# Share the created spreadsheet with the receiver.
56+
sh.share(user_mail, perm_type='user', role='writer')
57+
58+
59+
if __name__ == "__main__":
60+
parser = argparse.ArgumentParser(
61+
description="generate and share google sheet for give csv")
62+
parser.add_argument("-mail", help="Enter the email id of the community admin",
63+
dest="mail_id", type=str, required=True)
64+
parser.add_argument("-csv", help="Enter path of csv file",
65+
dest="csv", type=str, required=True)
66+
parser.add_argument("-spreadsheet_name", help="Enter name of spreadsheet",
67+
dest="ss_name", type=str, required=True)
68+
69+
args = parser.parse_args()
70+
71+
community_admin = args.mail_id
72+
csv_file = args.csv
73+
spreadsheet_name = args.ss_name
74+
75+
create_and_share_sheet(community_admin, spreadsheet_name, csv_file)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "trans-crawler-261018",
4+
"private_key_id": <your_private_key> ,
5+
"private_key": "",
6+
"client_email": "",
7+
"client_id": "",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": ""
12+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import gspread
2+
from oauth2client.service_account import ServiceAccountCredentials
3+
import pandas as pd
4+
import argparse
5+
import json
6+
7+
8+
def auth_user():
9+
# Define a authorized user with the credentials created.
10+
scope = ['https://www.googleapis.com/auth/spreadsheets',
11+
'https://www.googleapis.com/auth/drive']
12+
13+
credentials = ServiceAccountCredentials.from_json_keyfile_name(
14+
'credentials.json', scope)
15+
16+
client = gspread.authorize(credentials)
17+
return client
18+
19+
20+
def get_sheet_json(spreadsheet_name, json_file):
21+
# Retrieve spreadsheet by name from the user's google drive.
22+
client = auth_user()
23+
sheet = client.open(spreadsheet_name).sheet1
24+
data = sheet.get_all_records()
25+
26+
# Dump the retrieved csv file in json format.
27+
with open(json_file, 'w') as fout:
28+
json.dump(data, fout, indent=4)
29+
30+
31+
if __name__ == "__main__":
32+
parser = argparse.ArgumentParser(
33+
description="get data from spreadsheet in json format")
34+
parser.add_argument("-json", help="Enter path of json file",
35+
dest="json", type=str, required=True)
36+
parser.add_argument("-spreadsheet_name", help="Enter name of spreadsheet",
37+
dest="ss_name", type=str, required=True)
38+
39+
args = parser.parse_args()
40+
41+
spreadsheet_name = args.ss_name
42+
json_file = args.json
43+
44+
get_sheet_json(spreadsheet_name, json_file)
52.4 KB
Loading[フレーム]
53.3 KB
Loading[フレーム]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gspread==3.6.0
2+
oauth2client==4.1.3
3+
pandas==1.0.3
4+
argparse
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,ingredients,diet,prep_time,cook_time,flavor_profile,course,state,region
2+
Balu shahi,"Maida flour, yogurt, oil, sugar",vegetarian,45,25,sweet,dessert,West Bengal,East
3+
Boondi,"Gram flour, ghee, sugar",vegetarian,80,30,sweet,dessert,Rajasthan,West
4+
Gajar ka halwa,"Carrots, milk, sugar, ghee, cashews, raisins",vegetarian,15,60,sweet,dessert,Punjab,North
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[
2+
{
3+
"name": "Balu shahi",
4+
"ingredients": "Maida flour, yogurt, oil, sugar",
5+
"diet": "vegetarian",
6+
"prep_time": 45,
7+
"cook_time": 25,
8+
"flavor_profile": "sweet",
9+
"course": "dessert",
10+
"state": "West Bengal",
11+
"region": "East"
12+
},
13+
{
14+
"name": "Boondi",
15+
"ingredients": "Gram flour, ghee, sugar",
16+
"diet": "vegetarian",
17+
"prep_time": 80,
18+
"cook_time": 30,
19+
"flavor_profile": "sweet",
20+
"course": "dessert",
21+
"state": "Rajasthan",
22+
"region": "West"
23+
},
24+
{
25+
"name": "Gajar ka halwa",
26+
"ingredients": "Carrots, milk, sugar, ghee, cashews, raisins",
27+
"diet": "vegetarian",
28+
"prep_time": 15,
29+
"cook_time": 60,
30+
"flavor_profile": "sweet",
31+
"course": "dessert",
32+
"state": "Punjab",
33+
"region": "North"
34+
}
35+
]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /