Python 如何使用openpyxl普通工作簿查找列中的最后一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33541692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:32:06  来源:igfitidea点击:

How to find the last row in a column using openpyxl normal workbook?

pythonexcelopenpyxl

提问by human

I'm using openpyxl to put data validation to all rows that have "Default" in them. But to do that, I need to know how many rows there are.

我正在使用 openpyxl 对所有包含“默认”的行进​​行数据验证。但要做到这一点,我需要知道有多少行。

I know there is a way to do that if I were using Iterable workbook mode, but I also add a new sheet to the workbook and in the iterable mode that is not possible.

我知道如果我使用 Iterable 工作簿模式,有一种方法可以做到这一点,但我也在工作簿中添加了一个新工作表,并且在 iterable 模式下这是不可能的。

采纳答案by Charlie Clark

ws.max_rowwill give you the number of rows in a worksheet.

ws.max_row将为您提供工作表中的行数。

Since version openpyxl 2.4 you can also access individual rows and columns and use their length to answer the question.

从 openpyxl 2.4 版本开始,您还可以访问单个行和列并使用它们的长度来回答问题。

len(ws['A'])

len(ws['A'])

Though it's worth noting that for data validation for a single column Excel uses 1:1048576.

尽管值得注意的是,对于单列的数据验证,Excel 使用1:1048576.

回答by Taimoor Arif

find length of row and length of col

找到行的长度和 col 的长度

column:

colummn=sheet['A']
output tuple-->(A1,A2,A3........An)

len(colummn)
output length--> 18                    

for row length

对于行长

for i in sheet.iter_rows(max_row=0):

    print(len(i))

    break

This will give you length of header row where you put feature name . If you wan to get all rows length add max_row=len(colummmn) and remove break.

这将为您提供放置功能名称的标题行的长度。如果您想获得所有行的长度,请添加 max_row=len(colummmn) 并删除中断。