creating a worksheet using gspread - python

I am new to python. I was just trying to create a google worksheet using gspread. I read about using google api's from here. I downloaded credentials from Google Developers Console which is a file in json format. Then I used this code
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('spreadsheet1995.json', scope)
gc = gspread.authorize(credentials)
worksheet = gc.add_worksheet(title="A worksheet", rows="100", cols="20")
But it throws an error 'Client' object has no attribute 'add_worksheet' although I read documentation which included this attribute. Here is the link which I followed. Please help me sort out this problem.

First, you need to know to the difference between a spreadsheet and a worksheet. A spreadsheet is a container of worksheets. Like a book with many pages.
If you go to Google Sheets and hit the big "+" button you will start a new a new spreadsheet. This spreadsheet will at first contain a single worksheet named "Sheet1". You may add more worksheets to your spreadsheet.
Now back to gspread and your code sample.
To create a worksheet you need a spreadsheet. This is what you're missing in your code.
Skipping the authentication part, you code should look like this:
gc = gspread.authorize(credentials)
spreadsheet = gc.open("The name of your spreadsheet")
worksheet = spreadsheet.add_worksheet(title="A worksheet", rows="100", cols="20")
Now with worksheet you may read cell values, edit them, etc.
gspread API Reference

Related

Using google sheets through python in the most simple way

So basically I am making a discord bot that takes trades for ingame items on a game I play and stores the order in a google sheet. What would be the easiest way to do this through python, I know how to do all the bot stuff but when it comes to accessing a google sheet, searching through it and collecting certain rows of information I cant find much that helps. What module would I use to make this easiest as possible, the module needs to be able to search through the sheet for specific values in one column, find the first find the first empty cell in a column as well as collect all the information from a row. If anyone knows a good module for doing this it would be greatly appreciated.
Note: I have set up the OAuth and all that kind of stuff for the sheets api, I saw that there was a bunch of modules that make accessing the sheet easier however so I was wondering which one was the best at making the coding easier as I am not super experienced.
Use Googlesheets API to get the data and then pandas to read in the data as a dataframe. Once you have the dataframe, Pandas can accomplish this in various ways: "needs to be able to search through the sheet for specific values in one column, find the first empty cell in a column as well as collect all the information from a row"
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
import pandas as pd
SAMPLE_SPREADSHEET_ID = 'sheet ID' # your sheet ID
SAMPLE_NAME = 'sheet name' # your sheet name
RANGE = '!A1:D2' # your row/col sheet range
TOKEN_PATH = 'token.json' # path to your token file
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SAMPLE_RANGE_NAME = SAMPLE_NAME + RANGE
creds = Credentials.from_authorized_user_file(TOKEN_PATH, SCOPES)
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
df = pd.DataFrame(data=values[1:], columns=values[0])
Adapted from https://developers.google.com/sheets/api/quickstart/python

How do I get all values from an excel column and put them into an array in python?

I am creating a signup page for my app for a school project and I am using openpyxl to read an excel file and I do not know how to get all the data from one column into an array.
The file looks something like this.
And i want the array to look like this if it was printed:
[example1, example2, example3]
so that i can then check through to see if someone already has that username signed up.
sidenote: I know excel is not secure to use as a login database but it is for a school project so security is not really needed.
from openpyxl import load_workbook
# The source xlsx file is named as source.xlsx
wb=load_workbook("source.xlsx")
ws = wb.active
second_column = ws['B']
# Create the list
usernames = [cell.value for cell in second_column[1:]]

How to download a Google Docs excel sheet with a Gspread and access data locally (A1 notation)?

I need to download an excel sheet from Google Docs via Gspread and then multiple times I'll need to read the values of different cells in 'A1' notation. Thus, I can't just get the spreadsheet and then call val = worksheet.acell('B1').value, because the script will freeze out of too many API calls. My solution for now:
def download_hd_sheet():
worksheet = gc.values().get(spreadsheetId=excel_id, range='variables', valueRenderOption='FORMULA').execute()['values']
df = pd.DataFrame(worksheet)
writer = pd.ExcelWriter("Variables.xlsx", engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False, header=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
writer.save()
book = openpyxl.load_workbook('Variables.xlsx', data_only=False)
global hd_sheet
hd_sheet = book.active
So far what I'm doing is:
I download the values from the worksheet.
Transform it (list of lists) into a pandas dataframe.
Then I write the df to a .xlsx file.
I read the .xlsx file to a global variable
It seems to me that I am doing so many things just to achieve something that can be done in two lines. Please, let me know what would be more effective than the above.
I believe your goal as follows.
You want to download the Google Spreadsheet as the XLSX data.
You want to use the downloaded XLSX data without saving as the file.
You have already been able to get and put values for Google Spreadsheet using gspread.
You want to achieve this using python.
In order to achieve your goal, I would like to propose the following flow.
Download the Google Spreadsheet as the XLSX data using the method of Files: export in Drive API.
Open the XLSX data using the downloaded binary data with openpyxl.load_workbook().
Sample script:
In this sample script, from your situation, the access token is used from the authorization for gspread.
spreadsheetId = "###" # Please set the Spreadsheet ID.
client = gspread.authorize(credentials)
access_token = client.auth.token
url = "https://www.googleapis.com/drive/v3/files/" + spreadsheetId + "/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet"
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
book = openpyxl.load_workbook(filename=BytesIO(res.content), data_only=False)
hd_sheet = book.active
By above script, the XLSX data is directly downloaded from Google Spreadsheet and openpyxl.load_workbook
In this case, the following libraries in addition to gspread are used.
import openpyxl
import requests
from io import BytesIO
Note:
In this case, please include the scope of https://www.googleapis.com/auth/drive or https://www.googleapis.com/auth/drive.readonly. When you modified the scopes, please reauthorize the scopes. By this, the new scopes are reflected to the access token. So please be careful this.
References:
Files: export
Using openpyxl to read file from memory
I think that this thread might be useful for your situation.

Exporting scraped content to google sheets

I am willing to scrape a website for some information. It would be 3 to 4 columns. The difficult part is, i want to export all the data in to the google sheets and make the crawler run after some specific intervals. I 'll be using scrapy for this purpose. Any suggestions on how can i do this (by making custom pipeline or any other way as i don't have much experience in writing custom pipelines)
You can use the Google API and python pygsheets module.
Refer this link for more details Click Here
Please see the sample code and this might help you.
import pygsheets
import pandas as pd
#authorization
gc = pygsheets.authorize(service_file='/Users/desktop/creds.json')
# Create empty dataframe
df = pd.DataFrame()
# Create a column
df['name'] = ['John', 'Steve', 'Sarah']
#open the google spreadsheet (where 'PY to Gsheet Test' is the name of my sheet)
sh = gc.open('PY to Gsheet Test')
#select the first sheet
wks = sh[0]
#update the first sheet with df, starting at cell B2.
wks.set_dataframe(df,(1,1))

Xlwings - Delete similarly named sheets using loop

I am using xlwings to place stock data I pull from the internet into worksheets. The workbook opens with a Sheet1, and upon running my program creates various sheets named according to the stock index. This leaves Sheet1 and causes problems with other methods I want to call. I want to test for any sheets that contain Sheet (plus an integer) and subsequently delete it similar to how you would test for the presence of a list element using the in operator. How would I go about doing this in xlwings? Current xlwings methods only allow sheets in which you manually name to be deleted.
My attempts have been rather lackluster. I've been trying loops to recognize the sheet names but to no avail. Here is my attempt to do so.
import xlwings as xw
wb = xw.Book('practice.xlsx')
for sheet in wb.sheets:
if 'Sheet' in sheet:
xw.Sheet[sheet].delete()
This works:
import xlwings as xw
wb = xw.Book('practice.xlsx')
for sheet in wb.sheets:
if 'Sheet' in sheet.name:
sheet.delete()
is the syntax more like sheet.api.Delete()? My xlwings is broken right now to check the exact syntax.
You can check, if the given sheet exist and you can delete the existing sheet and add a new one.
import xlwings as xw
def df_to_excel_util(excel,sheet_to_dataFrame_map):
with xw.App(visible=False) as app:
wb = app.books.open(excel)
current_sheets = [sheet.name for sheet in wb.sheets]
for sheet_name in sheet_to_dataFrame_map.keys():
if sheet_name in current_sheets:
wb.sheets[sheet_name].delete()
new_sheet = wb.sheets.add(after=wb.sheets.count)
new_sheet.range('A1').value = sheet_to_dataFrame_map.get(sheet_name)
new_sheet.name = sheet_name
wb.save()
if you have sheet object, you can delete as given below
sheet.delete()
if you want to delete a sheet with a given name.
wb.sheets['sheet_name'].delete

Categories

Resources