does this library assume the Google Spreadsheet will have one sheet only? - python

I am trying to use this library to pull data from a Googlespreadsheet with two sheets in it, I can get data only from the first sheet but not the second sheet. sheet = client.open("sheetname").sheet1, if I change sheet1 to sheet2 I get the following error sheet = client.open("filename").sheet2 AttributeError: 'Spreadsheet' object has no attribute 'sheet2' how do I fix this? any help is appreciated!

.sheet1 is used as a shortcut.
In order to get the second sheet try that:
sheet = client.open("filename").get_worksheet(1)
1 means second sheet (starting from 0).
References:
Official documentation

In this case, you can use get_worksheet, worksheet and worksheets.
Sample script:
sh = client.open("###Spreadsheet name###") # or client.open_by_key(spreadsheetId)
worksheet = sh.get_worksheet(1) # Use the index of the sheet. 0 is the 1st sheet.
worksheet = sh.worksheet('Sheet2') # Use the sheet name of the sheet.
worksheet = sh.worksheets()[1] # In this case, all sheets are included in the array.
Note:
In the current stage, it seems that sh.sheet1 is only the 1st sheet.
Reference:
Selecting a Worksheet

Related

Update specific tab on google sheet - Python

I have the following code to append a dataframe in to a google sheet that runs everyday.
I had to create 03 more tabs in to this sheet and now, every time I upload the dataframe it goes to another tab and not the one that I need.
I`m using the following code to update the gsheet:
gc = gspread.authorize(credentials)
sh = gc.open_by_key("1O1NKT4LRf7F17kRjupUD7peonCwT04BG-l7pbo5-BLU").sheet1
values = df.values.tolist()
sh.append_rows(values)
I tried a few things such as
sh = gc.open_by_key("1O1NKT4LRf7F17kRjupUD7peonCwT04BG-l7pbo5-BLU").tabname
But it didnt work. Is there a way to do that?
thank you
Using sheet1 will give you the first worksheet in your spreadsheet, if your target sheet is not the first worksheet then you might need to use other methods to access that particular worksheet.
Best option is to get the worksheet by title (if you select worksheet using indexes, you need to update your code if ever you re-arranged your tabs. Hence the best option is to select worksheet by its title)
Here are all the options that you can use to select a worksheet using gspread:
Select worksheet by index. Worksheet indexes start from zero:
sh = gc.open_by_key("1O1NKT4LRf7F17kRjupUD7peonCwT04BG-xxxxxx")
worksheet = sh.get_worksheet(0)
Or by title:
worksheet = sh.worksheet("January")
Or the most common case: Sheet1:
worksheet = sh.sheet1
To get a list of all worksheets: (check each worksheet in the list based on their title)
worksheet_list = sh.worksheets()

How to parse only specific sheets in a workbook using openpyxl - or how to ignore empty sheets?

Well, this is actually a workaround for my main problem which is to "ignore the empty sheets in my workbook". I have found a way to print only those sheet names that are not empty. So, now I want to pass these names to my workbook and access only those sheets instead of every single sheet in wb. (I need to use openpyxl for this.)
I'm trying the below but it doesn't work:
wb = openpyxl.load_workbook("source_file.xlsx", data_only=TRUE)
for ws in wb.get_sheet_by_name(['Sheet1', 'Sheet2', 'Sheet4', 'Sheet5']):
for row in ws:
<do the necessary parsing operations here>
But this throws the below error:
"Worksheet ['Sheet1', 'Sheet2', 'Sheet4', 'Sheet5'] does not exist."
And if I pass the names separately, then it says:
TypeError: get_sheet_by_name() takes 2 positional arguments but 5 were given
Is there a way that I can tell it to access only specific sheets instead of every sheet in wb? Or better, is it possible to ignore all the empty sheets while parsing a .xlsx workbook?
You can store the sheet names in a list, and then iterate over that list to open each sheet:
import openpyxl
wb = openpyxl.load_workbook("source_file.xlsx", data_only=True)
sheets = ['Sheet1', 'Sheet2', 'Sheet4', 'Sheet5']
for sheet in sheets:
for row in wb[sheet]:
# <do the necessary parsing operations here>
Note that you can simply access a sheet from the workbook wb with wb[sheetname]. get_sheet_by_name() is deprecated. See the official documentation.

How to edit Excel (xlsx and xlsm) in python

I am very new to Python and this is my first project in python.
What I am doing is...
1. Retrieved the data from Sql server
2. Put the data in predefined excel template (specific worksheet).
3. If is there any data in this sheet then it should be replaced and only column name should remain in the sheet.
3. Another sheet in excel template contains a Pivot representation of data from step 2.
4. I need to refresh this pivot with new data from sheet1.
5. no of row in sheet1 can be changed depends on data from database.
I am fine with Step1 but unable oto perform excel operations.
I tried openpyxl but not able to much understand of it.
https://openpyxl.readthedocs.io/en/stable/
code:
from openpyxl import load_workbook
wb2 = load_workbook('CnA_Rec.xlsx')
print (wb2.sheetnames)
rawsheet = wb2.get_sheet_by_name('RawData')
print (rawsheet.cell_range)
Error with above code:
AttributeError: 'Worksheet' object has no attribute 'cell_range'
I can access individual cell but not range.
I need to select current range and replace it will new data.
ref link: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.cell_range.html
Can any one point me to some online example for the same or any sample code for this.
So, then let go for it with openpyxl. Where is your problem? This is a very basic start. We can change this script during the process.
import openpyxl
wb = openpyxl.load_workbook('hello_world.xlsx')
# do magic with openpyxl here and save
ws = wb.worksheets[0]
ws.cell(row=1, column=3).value = 'Hello' # example
ws.cell(row=2, column=3).value = 'World' # example
for i in range(2,20):
ws.cell(row=i,column=1).value = 'Row:' + str(i)
data = [ws.cell(row=i,column=1).value for i in range(1,11)]
print(data)
wb.save('hello_world.xlsx')

Python - Change the sheet index in excel workbook

I am trying to move an Excel sheet say of index 5 to the position of index 0. Right now I have a working solution that copies the entire sheet and writes it into a new sheet created at the index 0, and then deletes the original sheet.
I was wondering if there is another method that could push a sheet of any index to the start of the workbook without all the need of copy, create and write.
Maybe the function from XLRD module can help you
where you can get the sheet contents by index like this:
worksheet = workbook.sheet_by_index(5)
and then you can copy that into some other sheet of a different index, like this:
workbook.sheet_by_index(0) = worksheet

python gspread library only writes to worksheet labeled 'sheet1'

My sheet is named 'doc_name', and it has two worksheets, 'sheet1' and 'sheet2'. but, i can only write data to the worksheet labeled 'sheet1'?
is this a limitation or am i doing something wrong?
this works,
wks = gc.open("doc_name").sheet1
but this fails,
wks = gc.open("doc_name").sheet2
giving this error,
AttributeError: 'Spreadsheet' object has no attribute 'sheet2'
i also notice that this fails,
wks = gc.open("doc_name").Sheet1
...where i use a capital 'S'.. and it will only write if i specify lowercase .sheet1
how do i write to a worksheet without having to code... wks = gc.open("doc_name").sheet1?
This is because gspread only implemented sheet1 to let you retrieve the first sheet in your spreadsheet as a shortcut.
From the source code you can see the implementation of sheet1 is using get_worksheet(0)
#property
def sheet1(self):
"""Shortcut property for getting the first worksheet."""
return self.get_worksheet(0)
So if you want to retrieve other sheets, you need to use other methods like:
1.specify index as a integer indicating the position of the sheet to open starting from 0:
wks = gc.open("doc_name").get_worksheet(index)
or
2.specify title of the sheet to open as a string:
wks = gc.open("doc_name").worksheet(title)
That is to say, in you case, to get sheet2 you can probably use
wks = gc.open("doc_name").get_worksheet(1)
client = gspread.authorize(creds)
sheet = client.open('name').worksheet('name/title')

Categories