Write rows bottom in Google Sheet from Python - python

I am trying to write rows with python in one google sheet. I found function .insert_row() with that new rows are written in the top of my google sheet. How can I write it in the bottom? Any ideas?
That is how I am trying it now:
sheet.insert_rows(df_test.values.tolist())

I believe your goal is as follows.
In your situation, for example, the sheet has 1000 rows. The rows which are not empty are from 1st row to row 10.
You want to put the values from the next empty row that it's row 11. This is the 1st empty row.
From your script of sheet.insert_rows(df_test.values.tolist()), you want to achieve this using gspread for python.
In this case, how about the following modification? In this modification, append_rows method is used instead of insert_rows.
From:
sheet.insert_rows(df_test.values.tolist())
To:
sheet.append_rows(df_test.values.tolist(), value_input_option="USER_ENTERED")
Reference:
append_rows

Related

How to get rowname in every single page in Excel file using python?

I have a dataframe which has over thousands of rows in it.Then i am sending it to an Excel file using to_excel() And then I am printing a copy(Hardcopy) of the Excel file.As its a table the row name are only on the first page so its hard to figure which row is which in second or any other page.Is there any way so that I can get row names in every single page using python.(I am talking about a .xlsx file not .csv).Thanks
either you can programatically add a new column with row numbers, or you can go to page layout and ennable print headings. When you enable print heading, you will have the row numbers, but you will also have the Columns (A, B, C) getting printed. If that is ok then, using openpyxl it would look something like this.
openpyxl.worksheet.page.PrintOptions(horizontalCentered=None, verticalCentered=None, headings=None, gridLines=None, gridLinesSet=None)
All the None values are editable. So in this case it would look like this. heading=True
openpyxl.worksheet.page.PrintOptions(horizontalCentered=None, verticalCentered=None, headings=True, gridLines=None, gridLinesSet=None)
Further information and reading can be found here

Pandas: read rows from an excel file and write them to another one

Hi I'm pretty new to python and I'm trying to use pandas as described in the title.
My problem is very simple but I'm having some trouble with it: I need to read some rows from an excel file and, if the row's first column value it's not equal to the next row's first column value, I have to insert an empty row in the excel.
In order to do this I would like to read a row from my original excel, compare his value with the next one and, if they are the equal, write them in the other excel file. If they are different, I want to write an empty row, and then the different value row.
thanks in advance!

python: looping through indeces in Excel and replacing with string

So I have an excel sheet with the following format:
Now what I'm looking to do is to loop trough each index cell in column A and assign all cells the same value until the next 0 is reached. so for example:
Now I have tried importing the excel file into a pandas dataframe and then using for loops to do this, but I can't seem to make it work. Any suggestions or directions to the appropriate method would be much appreciated!
Thank you for your time
Edit:
Using #wen-ben's method: s.index=pd.Series((s.index==0).cumsum()).map({1:'bananas',2:'cherries',3:'pineapples'})
just enters the first element (bananas) for all cells in Column A
Assuming you have dataframe s using cumsum
s.index=pd.Series((s.index==0).cumsum()).map({1:'bananas',2:'cherries',3:'pineapples'})

Maintaining Formulae when Adding Rows/Columns

I'm doing some excel sheet Python automation using openpyxl and I'm having an issue when I try to insert columns or rows into my sheet.
I'm modifying an existing excel sheet which has basic formula in it (i.e. =F2-G2) however when I insert a row or column before these cells, the formula do not adjust accordingly like they would if you would perform that action in excel.
For example, inserting a column before column F should change the formula to =G2-H2 but instead it stays at =F2-G2...
Is there any way to work around this issue? I can't really iterate through all the cells and fix the formula because the file contains many columns with formula in them.
openpyxl is a file format library and not an application like Excel and does not attempt to provide the same functionality. Translating formulae in cells that are moved should be possible with the library's tokeniser but this ignores any formulae that refer to the cells being moved on the same worksheet or in the same workbook.
Easy, just iterate from your inserted row downward to the max row and change formulae's row number accordingly, below code is just a example:
#insert a new row after identified row
ws.insert_rows(InsertedRowNo)
#every time you insert a new row, you need to adjust all formulas row numbers after the new row.
for i in range (InsertedRowNo,ws.max_row):
ws.cell(row=i,column=20).value='=HYPERLINK(VLOOKUP(TRIM(A{0}),dict!$A$2:$B$1001,2,0),A{0})'.format(i)

How to delete/remove row from the google spreadsheet using gspread lib. in python?

I want to delete a record from a google spreadsheet using the gspread library.
Also, how to can I get the number of rows/records in google spreadsheet? gspread provides .row_count(), which returns the total number of rows, including those that are blank, but I only want to count rows which have data.
Since gspread version 0.5.0 (December 2016) you can remove a row with delete_row().
For example, to delete a row with index 42, you do:
worksheet.delete_row(42)
Can you specify exactly how you want to delete the rows/records? Are they in the middle of a sheet? Bottom? Top?
I had a situation where I wanted to wipe all data except the headers once I had processed it. To do this I just resized the worksheet twice.
#first row is data header to keep
worksheet.resize(rows=1)
worksheet.resize(rows=30)
This is a simple brute force solution for wiping a whole sheet without deleting the worksheet.
Count Rows with data
One way would be to download the data in a json object using get_all_records() then check the length of that object. That method returns all rows above the last non blank row. It will return rows that are blank if a row after it is not blank, but not trailing blanks.
worksheet.delete_row(42) is deprecated (December 2021). Now you can achieve the same results using
worksheet.delete_rows(42)
The new function has the added functionality of being able to delete several rows at the same time through
worksheet.delete_rows(42, 3)
where it will delete the next three rows, starting from row 42.
Beware that it starts counting rows from 1 (so not zero based numbering).
Reading the source code it seems there is no such method to directly remove rows - there are only methods there to add them or .resize() method to resize the worksheet.
When it comes to getting the rows number, there's a .row_count() method that should do the job for you.
adding to #AsAP_Sherb answere:
If you want to count how many rows there are, don't use get_all_records() - instead use worksheet.col_values(1), and count the length of that.
(instead of getting the entire table, you get only one column)
I think that would be more time efficient (and will definantly be memory efficient)

Categories

Resources