本文介绍了如何给工作表的范围作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Excel工作表,用于通过python openpyxl读取数据...因此在我的脚本中,我的值被硬编码为ws ['E2':'AB3'],因为AB3是最后一个条目可以读取...但是现在工作表已更新,现在最后一个单元格坐标是AR3 ...我如何使代码通用,以便每次工作表更新时代码都可以工作..?注意(我找出工作表中的总行数和列数...我该怎么办)
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)
推荐答案
注意:我想您总是想从'E2'开始.
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) )
这篇关于如何给工作表的范围作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!