I need to extract an image from a Google Sheets spreadsheet. It is not connected to a cell, and does not have an URL--it's just an inserted image:
How can I extract it using Python?
Thank you very much for an answer.
here is a part of a code:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import urllib
import numpy as numpy
scopes = ['spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json',scopes)
client = gspread.authorize(creds)
spreadsheet = client.open_by_url("docs.google.com/spreadsheets/d/…)
worksheet = spreadsheet.worksheet("Ark1")
list_of_lists = worksheet.range('A1:Q31')
print('{}\n'.format(list_of_lists))
Related
I have a CSV that I want to put into a google sheet into sheet3 of many. I was hoping someone can help me complete this code. I am using Google API. So far I have gotten the csv to upload to the google drive. Now I would like to change the code to update a specific google sheet in sheet3 instead of creating a new sheet. Bellow you will find the code that I am using to create a new sheet with the CSV data.
# Import Csv to Google Drive
import os
import glob
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)
# line used to change the directory
os.chdir(r'DIRECTORY OF CSV')
list_of_files = glob.glob('DIRECTORY OF CSV\*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
upload_file_list = [latest_file]
for upload_file in upload_file_list:
gfile = drive.CreateFile({'parents': [{'id': 'THE GOOGLE ID'}]})
# Read file and set it as the content of this instance.
gfile.SetContentFile(upload_file)
gfile.Upload() # Upload the file.
I believe your goal is as follows.
You want to put CSV data to the specific sheet of a Google Spreadsheet.
You want to achieve this using python.
You have already been able to get and put values to the Spreadsheet using Sheets API.
In this case, how about the following sample script?
Sample script 1:
When googleapis for python is used, how about the following sample script?
service = build("sheets", "v4", credentials=creds) # Please use your script for authorization.
spreadsheet_id = "###" # Please put your Spreadsheet ID.
sheet_name = "Sheet3" # Please put the sheet ID of the sheet you want to use.
csv_file = "###" # Please put the file path of the CSV file you want to use.
f = open(csv_file, "r")
values = [r for r in csv.reader(f)]
request = service.spreadsheets().values().update(spreadsheetId=spreadsheet_id, range=sheet_name, valueInputOption="USER_ENTERED", body={"values": values}).execute()
Sample script 2:
When gspread for python is used, how about the following sample script?
import gspread
import csv
client = gspread.oauth(###) # Please use your script for authorization.
spreadsheet_id = "###" # Please put your Spreadsheet ID.
sheet_name = "Sheet3" # Please put the sheet ID of the sheet you want to use.
csv_file = "###" # Please put the file path of the CSV file you want to use.
spreadsheet = client.open_by_key(spreadsheet_id)
worksheet = spreadsheet.worksheet(sheet_name)
f = open(csv_file, "r")
values = [r for r in csv.reader(f)]
worksheet.update(values)
Note:
About both sample scripts, the CSV data is retrieved from a CSV file on your local PC, and the CSV data is converted to a 2-dimensional array and put the array to "Sheet3" of Google Spreadsheet using Sheets API. In this sample script, Drive API is not used.
Reference:
Method: spreadsheets.values.update
I am trying to append this CSV file after the last row with data in this Google Sheet. But I can only overwrite the existing data.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
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"]
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'key.json', scope)
client = gspread.authorize(credentials)
spreadsheet = client.open('upload_data')
with open('gmt2.csv', 'r') as file_obj:
content = file_obj.read()
client.import_csv(spreadsheet.id, data=content)
When import_csv is used, it seems that the Spreadsheet is overwritten by the CSV data. I thought that this might be the reason of your issue. In your situation, how about using append_rows method? When your script is modified, it becomes as follows.
Modified script:
client = gspread.authorize(credentials)
# I modified below script.
sheetName = "Sheet1" # Please set the sheet name you want to append the CSV data.
spreadsheet = client.open('upload_data')
worksheet = spreadsheet.worksheet(sheetName)
content = list(csv.reader(open('gmt2.csv')))
worksheet.append_rows(content, value_input_option="USER_ENTERED")
In this case, import csv is used.
References:
append_rows
Method: spreadsheets.values.append
I'm looking for a way to save a google sheet as csv on my computer..
I tried this:
import gspread
gc = gspread.service_account(filename='client_secret.json')
sh = gc.open("sheets").worksheet("sheet1")
sh.to_csv("exported_file.csv")
How to make it work?
I would recommend using gsheets and oauth2client module for this.
from oauth2client.service_account import ServiceAccountCredentials
import gsheets
my_json = "client_secret.json"
my_sheet_url = f"https://docs.google.com/spreadsheets/d/{insert_id}"
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(my_json, SCOPE)
sheets = gsheets.Sheets(CREDS)
sheet = sheets.get(my_sheet_url)
sheet[0].to_csv("export.csv")
This saves the first worksheet as a csv next to your .py file
I currently have a script which reads a Google Sheet URL, and converts to a dataframe. To access this file, I need to authenticate each time I run the script. Is there an alternative which means I only have to do this once?
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
import pandas as pd
import matplotlib.pyplot as plt
wb = gc.open_by_url('...')
sheet = wb.worksheet('Sheet1')
data = sheet.get_all_values()
df = pd.DataFrame(data)
Thanks!
While uploading CSV file to Google drive, it automatically converting to Google Sheets. How to save it as CSV file in drive? or can I read google sheet through pandas data frame ?
Develop environment: Google Colab
Code Snippet:
Input
data = pd.read_csv("ner_dataset.desktop (3dec943a)",
encoding="latin1").fillna(method="ffill")
data.tail(10)
Output
[Desktop Entry]
0 Type=Link
1 Name=ner_dataset
2 URL=https://docs.google.com/spreadsheets/d/1w0...
WORKING CODE
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
worksheet = gc.open('Your spreadsheet name').sheet1
# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)
# Convert to a DataFrame and render.
import pandas as pd
pd.DataFrame.from_records(rows)
#Mount the Drive
from google.colab import drive
drive.mount('drive')
#Authenticate you need to do with your credentials, fill yourself
gauth = GoogleAuth()
#Create CSV and Copy
df.to_csv('data.csv')
!cp data.csv drive/'your drive'