may I know what I need to do to export the file in google sheet as xlsx format?
My code below is working but I need to save the file also into xlsx format...... :(
Here's my code:
from oauth2client.service_account import ServiceAccountCredentials
import gsheets
pdkey = "keypd.json"
url = f"https://docs.google.com/spreadsheets/d/1MCkqb_123123123123asdasdada/edit#gid=0"
SCOPE = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
CREDS = ServiceAccountCredentials.from_json_keyfile_name(pdkey, SCOPE)
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(url)
sheet[0].to_csv("/root/xlsx/SAMPLE.csv")
In your situation, how about exporting the Spreadsheet with the export method of Drive API? When this is reflected in your script it becomes as follows.
Modified script:
From:
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(url)
sheet[0].to_csv("/root/xlsx/SAMPLE.csv")
To:
access_token = CREDS.create_delegated(CREDS._service_account_email).get_access_token().access_token
url = "https://www.googleapis.com/drive/v3/files/" + spreadsheet_id + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet"
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
# If you want to create the XLSX data as a file, you can use the following script.
with open("sample.xlsx", 'wb') as f:
f.write(res.content)
In this script, please add import requests.
In this modified script, the Spreadsheet is exported as XLSX data using the method of export in Drive API. The access token is retrieved from the service account.
Reference:
Files: export
Related
I created the following code in google colab - I don't know how to get the data from the URL response (symbol, bidPrice, askPrice) to the export code to google shets - the export code works for me. I will be very grateful for your answer.
import requests
import json
url = "https://api.binance.com/api/v3/ticker/bookTicker?symbol=BNBUSDT"
payload={}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
{"symbol":"BNBUSDT","bidPrice":"272.30000000","bidQty":"410.84300000","askPrice":"272.40000000","askQty":"228.60000000"}
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
# Open our new sheet and add some data.
worksheet = gc.open('16,09').sheet1
cell_list = worksheet.range('L1:N2')
import random
for cell in cell_list:
cell.value = random.randint(1, 10)
worksheet.update_cells(cell_list)
# Go to https://sheets.google.com to see your new spreadsheet.
I have created a new google sheet using google api. I was able to create it if I have one file for all the below tasks:
Create credential and create an instance of sheets
Create a new sheet with a title
Add permissions to the sheet so that I can access it
Below code runs successfully :
## Define scopes and service account file - these are like ID and password for google api
SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = 'keys.json'
## Authorizing to connect with google api scopes(these scopes should be enabled in google api)
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
spreadsheet_id = Null
spreadsheet = {
'properties': {
'title': 'Test'
}
}
# connecting to google sheets api
service = build('sheets', 'v4', credentials = creds)
# Call the Sheets API
sheet = service.spreadsheets()
# Create spreadsheet with title
create_response = sheet.create(body = spreadsheet, fields='spreadsheetId').execute()
spreadsheet_id = create_response.get('spreadsheetId')
# Add permissions to account/emails
permits = [{'role': 'writer', 'type':'user', 'emailAddress': 'itika.sharma10#gmail.com'}]
# connecting to google drive api
drive = build('drive', 'v3', credentials = creds)
for permit in permits:
res = drive.permissions().create(fileId = spreadsheet_id, body = permit, fields="id").execute()
Now, I wanted to have separate files and functions for all these tasks like below:
Credentials.py - to create sheet instance
Permissions.py - to add permissions
Create.py - to create a new sheet and I am calling the other two files from this file
SCOPES = [['https://www.googleapis.com/auth/spreadsheets'], ['https://www.googleapis.com/auth/drive']]
spreadsheet_id = Null
spreadsheet = {
'properties': {
'title': 'Test'
}
}
def create_sheet(spreadsheet):
# connecting to google sheets api
service = build('sheets', 'v4', credentials = creds)
# Call the Sheets API
sheet = service.spreadsheets()
# Create spreadsheet with title
create_response = sheet.create(body = spreadsheet, fields='spreadsheetId').execute()
spreadsheet_id = create_response.get('spreadsheetId')
return(spreadsheet_id)
if __name__ == '__main__':
creds = credentials.create_creds(SCOPES)
spreadsheet_id = create_sheet(spreadsheet)
permissions.add_permissions(spreadsheet_id, SCOPES)
But I get below error:
Exception has occurred: TypeError
sequence item 0: expected str instance, list found
File "C:\Users\itika\Desktop\Python\web\Untitled Folder\create.py", line 25, in create_sheet
create_response = sheet.create(body = spreadsheet, fields='spreadsheetId').execute()
File "C:\Users\itika\Desktop\Python\web\Untitled Folder\create.py", line 32, in <module>
spreadsheet_id = create_sheet(spreadsheet)
I checked the type of creds, spreadsheet and sheet for both the codes and compared them. They are identical.
Can someone help to resolve this error.
I am new to stackoverflow, so I sorry in advance if I do something wrong
I have a spreadsheet on Google sheets, for example, this one
And there is a link in the cell inside the href tag. I want to get the link and the text of the cell using Google Sheets API or gspread.
I have already tried this solution but I get access token 'None'.
I have tried to web scrape with beautifulsoup, but it didn't work as well.
As for bs4 solution, I tried using this code, that I found here
from bs4 import BeautifulSoup
import requests
html = requests.get('https://docs.google.com/spreadsheets/d/1v8vM7yQ-27SFemt8_3IRiZr-ZauE29edin-azKpigws/edit#gid=0').text
soup = BeautifulSoup(html, "lxml")
tables = soup.find_all("table")
content = []
for table in tables:
content.append([[td.text for td in row.find_all("td")] for row in table.find_all("tr")])
print(content)
I figured it out. Here's the full code if anyone needs it
import requests
import gspread
import urllib.parse
import pickle
spreadsheetId = "###" # Please set the Spreadsheet ID.
cellRange = "Yoursheetname!A1:A100" # Please set the range with A1Notation. In this case, the hyperlink of the cell "A1" of "Sheet1" is retrieved.
with open('token_sheets_v4.pickle', 'rb') as token:
# get this file here
# https://developers.google.com/identity/sign-in/web/sign-in
credentials = pickle.load(token)
client = gspread.authorize(credentials)
# 1. Retrieve the access token.
access_token = client.auth.token
# 2. Request to the method of spreadsheets.get in Sheets API using `requests` module.
fields = "sheets(data(rowData(values(hyperlink))))"
url = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheetId + "?ranges=" + urllib.parse.quote(cellRange) + "&fields=" + urllib.parse.quote(fields)
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
print(res)
# 3. Retrieve the hyperlink.
obj = res.json()
print(obj)
link = obj["sheets"][0]['data'][0]['rowData'][0]['values'][0]['hyperlink']
print(link)
UPDATE!!
More elegant solution is this. Creating service:
CLIENT_SECRET_FILE = 'secret/secret.json'
API_SERVICE_NAME = 'sheets'
API_VERSION = 'v4'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
def Create_Service():
cred = None
pickle_file = f'secret/token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
if os.path.exists(pickle_file):
with open(pickle_file, 'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
cred = flow.run_local_server()
with open(pickle_file, 'wb') as token:
pickle.dump(cred, token)
try:
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
print(API_SERVICE_NAME, 'service created successfully')
return service
except Exception as e:
print('Unable to connect.')
print(e)
return None
service = Create_Service()
And extracting links from each sheet in a spreadsheet in a form of convenient dictionaries
fields = "sheets(properties(title),data(startColumn,rowData(values(hyperlink))))"
print(service.spreadsheets().get(spreadsheetId=self.__spreadsheet_id,
fields=fields).execute())
So, how fields work. We go to Spreadsheet object description and looking for JSON representation. If we want to return, for example sheet object from that json representation, we just use this fields = "sheets", because Spreadsheet has field "sheets" it its json representation.
Ok, cool. We got sheets object. How to access sheet object fields? Just click on that thing and look for its fields.
So, how to combine fields? It's easy. For example, I want to return field "properties" and "data" from sheets object, I write the fields string that way: fields = "sheets(properties,data)". So we just list them as arguments in an ordinary function but without space.
The same applies for objects that return data fields and ect.
My Goal is to read a .csv file from google drive and load it to a dataframe.
I tried some answers here but the thing is, the file is not public and needs authentication.
I looked up on goggle drive API but I was stuck there and I don't know how to move forward. I did manage to open google sheet and load it to a dataframe but that is different, this is a sample for google sheet that works.
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
sheets_file = sheet.values().get(
spreadsheetId=sheet_id,
range=sheet_range
).execute()
header = sheets_file.get('values', [])[0] # Assumes first line is header!
values = sheets_file.get('values', [])[1:] # Everything else is data.
if not values:
print('No data found.')
else:
all_data = []
for col_id, col_name in enumerate(header):
column_data = []
for row in values:
column_data.append(row[col_id])
ds = pd.Series(data=column_data, name=col_name)
all_data.append(ds)
df = pd.concat(all_data, axis=1)
print(df.head())
I saw some google colab methods too but I cant use that as I am restricted to using python only, any Idea on how to approach this?
I believe your goal and situation as follows.
You want to download the CSV data from the CSV file on Google Drive.
You can get values from Google Spreadsheet using googleapis for python.
Pattern 1:
In this pattern, the CSV data is downloaded with googleapis. The downloaded CSV data is saved as a file. And the value is retrieved by the method of "Files: get" in Drive API v3.
Sample script:
file_id = "###" # Please set the file ID of the CSV file.
service = build('drive', 'v3', credentials=creds)
request = service.files().get_media(fileId=file_id)
fh = io.FileIO("sample.csv", mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
In this case, the CSV data can be converted to the dataframe with df = pd.read_csv("sample.csv").
Pattern 2:
In this pattern, as a simple method, the access token is used from creds. The downloaded CSV data is not saved as a file. And the value is retrieved by the method of "Files: get" in Drive API v3.
Sample script:
file_id = "###" # Please set the file ID of the CSV file.
access_token = creds.token
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
print(res.text)
In this case, the CSV data can be directly converted to the dataframe with df = pd.read_csv(io.StringIO(res.text)).
Note:
In the following scripts, please include the scope of https://www.googleapis.com/auth/drive.readonly and/or https://www.googleapis.com/auth/drive. When you modified the scopes, please reauthorize the scopes. By this, the modified scopes are included in the access token. Please be careful this.
Reference:
Download files
I'm interested in uploading a csv to google sheets/drive to create a shareable spreadsheet. Reading through https://developers.google.com/sheets/api/samples/ I'm not sure how. Any advice would be appreciated
If you already have access token, please skip following 3 steps.
Retrieve Client ID, Client Secret at Google Developers Console. Scope is "https://www.googleapis.com/auth/drive".
Retrieve code using Client ID.
Retrieve access token using Client ID, Client Secret, code and redirect URI.
Reference is https://developers.google.com/identity/protocols/OAuth2#scenarios.
Python script:
import json
import requests
accessToken = "#####"
csvfile = "#####"
headers = {"Authorization": "Bearer " + accessToken}
metadata = {
"name": csvfile,
"mimeType": "application/vnd.google-apps.spreadsheet"
}
files = {
"data": ("metadata", json.dumps(metadata), "application/json"),
"file": (csvfile, open(csvfile, "rt"), "text/csv")
}
requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
This is a very simple sample script to upload a CSV file and convert to spreadsheet with CSV file name. If you want to change file name of spreadsheet, please change "name". If I misunderstand what you want, I apologize.