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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 19:13:09

问题


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.

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.


回答1:


ws.max_row will give you the number of rows in a worksheet.

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

len(ws['A'])

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




回答2:


find length of row and length of 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



来源:https://stackoverflow.com/questions/33541692/how-to-find-the-last-row-in-a-column-using-openpyxl-normal-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!