Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 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) )  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...