I have google spreadsheet with different tabs. These hold quarterly metrics, e.g. 1Q14, 2Q14, etc.
I want to read these data and convert it to a Pandas DataFrame.
I managed to write code to open the spreadsheet with GSP.open
see first code snippet
Then I want to iterate over all sheets and extract their names from the list I obtain with:
sheets = spreadsheet.worksheets()
list of sheets returned
I want to get the first part, the name, not the id. I know I can obtain the full first entry in the list with sheet[0], but now, how do I get the name only, without the id?
How do I access the sheet name in that object that is returned? I can't find it - appreciate your help. Thank you, Marc
Just access the property title of the sheet, like this:
sheets = spreadsheet.worksheets()
for sheet in sheets:
sheet.title
Reference:
gspread: API Reference
Related
I'm trying to find a value exist in the GSheet. Connection to Google sheet works. I can fetch all the worksheets within the files, but I can't some reason iterable the list of worksheets.
Here is the file looks likes:
I'm trying to find cell value and then fetch the values within that column.
Sample Code:
#Gsheet setup w/ key.json token
scope = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('gdoc.json',scope)
gc = gspread.authorize(credentials
sheet = gc.open("GSHEET_Search_test")
cell = sheet.find("B02")
print(cell)
Getting errors:
I was able to iterate over the worksheet index value worksheet = sheet.get_worksheet(counter)
I'm writing data in a Google Sheet using this function :
def Export_Data_To_Sheets(df):
response_date = service.spreadsheets.values().update(
spreadsheetId=SAMPLE_SPREADSHEET_ID_input,
valueInputOption='RAW',
range=SAMPLE_RANGE_NAME,
body=dict(
majorDimension='ROWS',
values=df.T.reset_index().T.values.tolist()[1:])
).execute()
print('Sheet successfully Updated')
It works well, but I have two tabs in my Google Sheet and I would like to choose in which one I want to write data. I don't know how can I do this.
In this point in the code:
range=SAMPLE_RANGE_NAME
You can replace this value with a sheet and cell reference, something like:
range="Sheet1!A1:D5"
Reference
Writing a Single Range
Can't seem to find any answer to this, but are there any functions/methods which can get a worksheet ID?
Currently, my code looks like this:
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
....code to authorize credentials goes here....
sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet))
client.import_csv('abcdefg1234567abcdefg1234567', contents)
but I don't want to hardcode the abcdefg1234567abcdefg1234567. Is there anything I can do, like sheet.id()?
I believe your goal as follows.
In order to use import_csv, you want to retrieve the Spreadsheet ID from sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet)).
You want to achieve this using gspread with python.
In this case, you can retrieve the Spreadsheet ID from client.open(str(self.googleSheetFile)). So please modify your script as follows.
From:
sheet = client.open(str(self.googleSheetFile)).worksheet(str(self.worksheet))
client.import_csv('abcdefg1234567abcdefg1234567', contents)
To:
spreadsheet = client.open(str(self.googleSheetFile))
sheet = spreadsheet.worksheet(str(self.worksheet))
client.import_csv(spreadsheet.id, contents)
Note:
When I saw the document of gspread, it says as follows. So please be careful this.
This method removes all other worksheets and then entirely replaces the contents of the first worksheet.
This modified script supposes that you have already been able to get and put values for Google Spreadsheet using Sheets API with gspread.
Reference:
import_csv(file_id, data)
As above I'm trying to update more than one sheet file with the same information using the sheet IDs as the identifier. I''ve attempted to achieve this with for loops with no success. Some of the basic code that I'm trying to achieve this with is:
conn = gspread.authorize(credentials)
sheets = ['sheetid1', 'sheetid2']
worksheet_list = conn.open_by_key(sheets).worksheet("Rack Layout")
worksheet_list.update_acell('Q1', 'some cell value')
So if I define 'sheets' as one sheetid the updates work fine, however if I define 'sheets' as more than one sheet I get the error. I know this is really basic and I think the issue is its trying to open the full list (i.e. both sheetids) on the same line, rather than saying 'run this line for one sheetID and then the next'. I think the way to achieve this is a for loop, but I've yet to get this to work with a for loop. I know this is a nube question, but I've been trying for a while now, and from my searching I haven't found any other forum posts about getting gspread to do this.
You have 2 Google Spreadsheets.
2 Google Spreadsheets have the sheet of "Rack Layout", respectively.
You want to put the value of "some cell value" to the cell "Q1" in the sheet of "Rack Layout" for each Spreadsheet.
You want to achieve this using gspread with python.
You have already been able to get and put values for Spreadsheet using Sheets API.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this modified script, the value is put to each Spreadsheet using the for loop.
Modified script:
conn = gspread.authorize(credentials)
sheets = ['sheetid1', 'sheetid2']
for sheet in sheets: # Added
worksheet_list = conn.open_by_key(sheet).worksheet("Rack Layout")
worksheet_list.update_acell('Q1', 'some cell value')
If I misunderstood your question and this was not the direction you want, I apologize.
I have this:
dic_sheets = {}
for y in xl_files[]
dic_sheets.update({y:[]})
I want to populate the tables in the dictionary (dic_sheets) for each key(y) with the individual sheets inside of the excel document.
I do not know how many sheets are inside of the excel document; I don't have an index number to stop a range (x,y,z) loop.
Another way to put it: I want to dump x-number of excel files into the active directory and have each files sheets populate in a dictionary when I run the .py in CMD.
Can anyone help me achieve this goal?
xl_files contains "ExcelFile" data "pandas.io.excel.ExcelFile object at 0x0FF6B0D0
Edit: y represents individual excel files
Edit2: I need only the sheet names (or their unique index numbers) to populate, (i.e. 'sheet1', 'pivot2'). I'm not yet concerned with cells in the sheets.
Edit3: I already have the table ‘xl_files’ generated to contain every excel file in the cwd
I figured it out!
I had to use a for loop and the return function as an object, then combine it with another object of the array.append function and return function with a new array.
I'll try to word my questions better in the future, as I did not get a bite this round.