The excel file has content in A1, A2 , A3. I want python to automatically write the output in first empty cell in column A .i.e it should write on A4
Another example - lets say if I have content written from B1 to B130. Here I would like python to write the desired result in cell B131.
How do I form a python solution that can perform this task in excel through xlwings ?
if your data is continuous, get the end of the current region to get the last cell then offset the cell by one to get the next empty cell.
cel = Range("A1:A2")
rng = cel.current_region
last_cel=rng.end("down")
empty_cell= last_cel.offset(1,0)
now you can do what you want with the empty_cell
Related
I'm trying to split some part of DataFrame from csv.
I'd like to export only (17,A), (18,A) in the csv file. But The csv file is only one example. I mean, we only know about a1 and a2. a1 is always on (1,A) and a2 is on (random, A). so I have no clue about what position p and q are on it. Also, they might not be p and q. They are made like random alphabets.
So I mean, the data I want are below a2 and I want to separate all the data above the first NaN value below a2.
I am using openpyxl to work with excel on python.
I have a list i want to add each value inside it in excel file, my current code:
for y in myzoo:
loo1 = str(y)
c5a = my_sheet.cell(row= 21 , column = 3)
c5a.value = loo1
myzoo is the list (its originally a pyodbc.Row)
and i convert each entry to string, then save it to excel file, the problem is currently it save only last one overwriting all earlier values, i want to do one of two: save each value in next empty cell in row, or even (which less preferable) saving all the exported data into the cell without deleting earlier ones, thanks.
I think you can just do something like this:
column = 3 # start column
while myzoo:
c5a = my_sheet.cell(row=21, column=column)
if not c5a.value:
c5a.value = str(myzoo.pop(0))
column += 1
in case you need to preserve myzoo - you will need to copy it. (temp = myzoo.copy())
I have excel files in following format:
Sensor 1 meta
Sensor 2 meta
"Summary of Observation"
Sensor 1
Sensor 2
The number of rows before and after "Summary of Observation" is not fixed (i.e one file may have only sensor 1,2 while other may have 1,2,3....)
In dataframe, I only want information after "Summary of Observation")
Right now, I open the excel file, note the row from which I want information and parse it in
df = pd.read_excel("1.xlsx",skiprows = %put some value here%)
Is there a way to automate this, i.e. I don't want to open excel. Rather only import relevant rows (or delete them after importing).
After importing the file you can find index and select a data from that point.
# I used column name as `text` you can replace it with yours
idx = df[df['text']=='Summary of Observation'].index[0]
df = df[idx+1:]
print(df)
Output:
text
3 Sensor 1
4 Sensor 2
Or if you want to include Summary of Observation just use idx in place of idx+1
you can open the excel and use df.loc[df[0]=="Summary of Observation"].index[0] to get the index
Working code at https://github.com/gklc811/Python3.6/blob/master/stackoverflowsamples/excel.ipynb
I'm trying to automate a daily report we have, and I'm using a query to pull in data and writing it in Excel using openpyxl, and then doing a vlookup in openpyxl to match a cell value. Unfortunately I'm hung up on how to iterate through the rows to find the cell value to look up.
for row in ws['E5:E91']:
for cell in row:
cell.value = "=VLOOKUP(D5, 'POD data'!C1:D87, 2, FALSE)"
It works except I don't know how to change the D5 value to look up D6, D7, D8, etc. depending on the row I'm on. I'm honestly at a loss for how to best approach this. Obviously I don't feel like writing the formula out for every single row, and there's other columns I'd like to do this for once I get it.
Using your example, you can do:
for row in ws['E5:E91']:
for cell in row:
cell.value = "=VLOOKUP(D{0}, 'POD data'!C1:D87, 2, FALSE)".format(cell.row)
I'm reading the documentation for openpyxl, and I needed something a bit more specific and I wasn't sure if there's a way to do it using iter_rows or iter_cols.
In the docs, it said to do this to grab rows and cols:
for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):
for cell in row:
print(cell)
or
for col in ws.iter_cols(min_row=1, max_col=3, max_row=2):
for cell in col:
print(cell)
Doing this will give me A1, B1, C1 and so on or A1, A2, B1, B2, and so on.
But is there a way to manipulate this so you can grab the data from another point in the sheet?
I'm trying to grab the cells from F3 to W3 for example. But I'm not sure how many rows there are, there could be 5, there could be 10. So I would need to grab F4 to W4 and so on until I reach the last one which could be F10 to W10 or something.
I understand how the iter_rows and iter_cols work but I haven't found a way to manipulate it to start elsewhere and to reach an end if there are no other values left? It appears I would have to define the max_rows to how many rows there are in the sheet. Is there a way for it to check for the max amount of rows itself?
The biggest question I have is just how to iterate through the rows starting in the middle of the sheet rather than the beginning. It doesn't have to use iter_rows or iter_cols, that's just the part I was reading up on most in the documentation.
Thank you in advance!
What's wrong with ws.iter_cols(min_row=3, min_col=6, max_col=23) for ws[F3:W…]? If no maximum is specified openpyxl will keep iterating as far as it can.
If you wish to be able to dynamically end when you've reached the end of data (so, if you end up with a sheet with more than 23 rows / columns, for example), you can set max_row=ws.max_row and max_col=ws.max_column