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
Related
Short description of the problem:
I am currently accessing an Excel workbook from Python with openpyxl.
I have some dynamic spill formulas in sheet1, like filter(), byrow() and unique().
With the python script, I am doing some operations in sheet2, but I am not touching sheet1 (where the dynamic spill formulas are located).
When using workbook.save() method in Python, I experience that the dynamic formulas in sheet1 are ruined and static, not having the dynamic functionality they had before interacting with python.
What can I do? Use a parameter in .save()? Use another method?
Detailed description of problem (with pictures):
I have a workbook called Original, with the following three sheets:
nums
dynamic
dump
In "nums" I have a cell for ID (AA), and a column with some numerical values (picture1).
In "dynamic" I have some dynamic formulas like byrow() and filter() that updates automatically with the values in ID and Values-column of "nums" (picture2).
The sheet "dump" is for now empty.
I have a second workbook called Some_data, which have one sheet with a 3-column dataframe (picture3).
I am dumping the 3-column dataframe of Some_data into the empty "dump"-sheet of Original with a Python script, and then using the workbook.save() method to save the new workbook.
The code is here:
import pandas as pd
from openpyxl import load_workbook
Some_data = filepath of the workbook
Original = filepath of the workbook
df = pd.read_excel(Some_data, engine = "openpyxl")
wb = load_workbook(filename = Original)
ws = wb["dump"]
rownr = 2
for index, row in df.iterrows():
ws["B"+str(rownr)] = row["col1"]
ws["C"+str(rownr)] = row["col2"]
ws["D"+str(rownr)] = row["col3"]
rownr+=1
wb.save(filepath of new workbook)
Now, the newly saved workbook's sheet "dump" has now been populated.
The problem is that the dynamic formulas in the sheet "dynamic" has been ruined, although the python script does not interact with any of the sheets "nums" or "dynamic".
First of all - the dynamic array formulas (like filter) now have brackets around them (picture4), and the dynamic array formulas are not dynamic anymore (there are no blue line around the array when selected, and they do not update automatically; picture5).
I need help with what to do. I want to save the excel-file, but with the dynamic array formulas not being ruined.
Thank you for your help, in advance.
Frode
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()
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
I am looking for a way to append data from a Python program to an excel sheet. For this, I chose the openpyxl library to save this data.
My problem is how to put new data in the excel file without losing the current data, in the last row of the sheet. I look into the documentation but I did not see any answer.
I do not know if this library has a method to add new data or I need to make a logic to this task.
The last row of the sheet can be found using max_row():
from openpyxl import load_workbook
myFileName=r'C:\DemoFile.xlsx'
#load the workbook, and put the sheet into a variable
wb = load_workbook(filename=myFileName)
ws = wb['Sheet1']
#max_row is a sheet function that gets the last row in a sheet.
newRowLocation = ws.max_row +1
#write to the cell you want, specifying row and column, and value :-)
ws.cell(column=1,row=newRowLocation, value="aha! a new entry at the end")
wb.save(filename=myFileName)
wb.close()
What you're looking for is the Worksheet.append method:
Appends a group of values at the bottom of the current sheet.
If it’s a list: all values are added in order, starting from the first column
If it’s a dict: values are assigned to the columns indicated by the keys (numbers or letters)
So no need to check for the last row. Just use this method to always add the data at the end.
ws.append(["some", "test", "data"])
I want to take a dataframe and have it replace the existing data in a specific sheet let's say sheet1. Then I want to take another dataframe and have it append to the data on another sheet lets say sheet2 in the same workbook.
Currently, I have to write in the data into a new excel workbook. I have not been able to find online how to actually replace a sheet within a saved workbook on my machine and/or append another sheet. I can do one after the other if needed.
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter') # this output is where my df goes
df.to_excel(writer, sheet_name='Sheet1') # the data goes on this sheet
writer.save()
So the result would be the original workbook, with the same sheets , but with completely new data on the sheets I specified or appended data.