how to give range of a worksheet as variable

2024/10/5 20:49:14

I am having one excel sheet which is used to read the data through python openpyxl...so in my script i have values that are hard coded as ws['E2':'AB3'] as AB3 is the last entry to be read...but now the sheet is updated and now last cell coordinate is AR3...how can i make the code generic so that every time sheet updates the code should work..? Note(i find out total number of rows and column in sheet...what can i do)

rows = ws.max_row
column = ws.max_column
print(rows,column)
column_letter = get_column_letter(column)
print(column_letter)
Answer

Question: how can i make the code generic

Note: I assume you always want to start at 'E2'.

from openpyxl.utils import range_boundaries
# Define only start 'E2',     
# all max_* values, last Row, Column, are returned by Default
min_col, min_row, max_col, max_row = range_boundaries('E2')# Iterate all Rows, starting at min_row == 2
for columns in worksheet.iter_rows(min_col=min_col, min_row=min_row, max_col=max_col, max_row=max_row):# Iterate all Columns, starting at min_col == 'E'for cell in columns:print('%s: cell.value=%s' % (cell, cell.value) )  
https://en.xdnf.cn/q/119743.html

Related Q&A

how to remove brackets from these individual elements? [duplicate]

This question already has answers here:How do I make a flat list out of a list of lists?(32 answers)Closed 2 years ago.This post was edited and submitted for review 2 years ago and failed to reopen th…

First project alarm clock

from tkinter import * from tkinter import ttk from time import strftime import winsoundclock = Tk()clock.title("WhatAClock")clock.geometry("300x400")notebook = ttk.Notebook()tab1_t…

Invalid Syntax using @app.route

Im getting a Invalid Syntax in line 22 @app.route(/start) and really dont know why... Im developing it under a Cloud9 server https://c9.io , maybe that has something to do with it... I tried it in two …

How do I count unique words using counter library in python?

im new to python and trying various librariesfrom collections import Counter print(Counter(like baby baby baby ohhh baby baby like nooo))When i print this the output I receive is:Counter({b: 10, : 8, …

Need some debugging in my program: filling up SQL tables with data retrieved from a Python program

I am filling up SQL tables with data that I have retrieved from a Python program. I am using Visual Studio Code for the Python program and MySQL Workbench 8.0 for SQL. There are some errors in it that …

How do I create a magic square matrix using python

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the egg…

Ensuring same dimensions in Python

The dimensions of P is (2,3,3). But the dimensions of M is (3,3). How can I ensure that both P and M have the same dimensions i.e. (2,3,3). import numpy as np P=np.array([[[128.22918457, 168.52413295,…

how to stop tkinter timer function when i press button one more times?

id tried to use root.after_cancel(AFTER), but i dont know how.root.after_cancel(AFTER) AFTER = None def countdown(count,time,name):global AFTERtime[text] =name,":",datetime.fromtimestamp(cou…

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…