Python Forum
opepyxl not counting rows in spreadsheets?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
opepyxl not counting rows in spreadsheets?
#1
Prepare a spreadsheet with anything in cells A1..A5
Save as 'Myfile.xlsx?
run this code:
    import openpyxl

    wb = openpyxl.load_workbook('Myfile.xlsx')
    sheet = wb.get_sheet_by_name('My sheet')
    print(sheet.max_row)
Answer 5. OK

Now delete A5 in Myfile and save.
Run again the code.
Answer 5 (not 4). Why?
Reply
#2
As the name suggest and also described in the docs:
Quote: max_row - The maximum row index containing data (1-based)

i.e. that is not number of rows with data, but max row index (1-based)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Excuse me buran, but it seems that you have not understood my question. When you delete A5 certainly you shorten column A, but this is irrelevant here. What is relevant is that there are no filled cells in row 5. This is what the user sees, but openpyxel does not. And still further.
There are two ways to erase A5. You can select the cell and delete it, or yo can selecte another empty cell, for instance A6, and drag the cross in the the lower right corner to A5. There are 4 rows in both cases. Openpyxl detects the second case, but not the first. This is an anomally that could amount to a bug.
I am not able to determine the exact cause, but I guess it could come either from a cache efect, or from the different inner workings of excel in both cases. It could be also dependent from the excel version. Mine is 2006.
Hope I have made myself clear.
Reply
#4
Indeed, I misunderstood your question. I was under impression you clear content in cell A1. My bad, sorry.

Now, however, I tried to replicate your problem and was not able to do so. Using openpyxl 2.6.3. Openpyxl correctly detects max row number, regardless how A5 is deleted - select the cell and press Del, drag empty cell from row 6, delete the cell and move cells up, delete entire row, etc.

Are you sure you save the workbook after you delete the A5 cell/content? Make sure you don't have something in rest of the cells...
I doubt excel version will have any effect (i.e. openpyxl does not rely on MS Excel to read the file). different/older openpyxl may be the reason (if there was bug in a version before 2.6.3)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I have checked and recheked the operation, and I stand by what I wrote. There is no possibility of error. I have 3 windows open in my screen: the 2 excel files, and the Pyscripter window. I created the first excel file, and saved it. Then saved it 'save as' with a new name that differs from the first one just in one character. Then I perform the 2 delete operations in parallel in both files and save both files. Then I run the script in both files. What I have now in my screen is 2 visually identical files with a different row count in python. I also saved the wrong file 3 more times just for if this could force to empty a memory caché; to no avail.No mistake possible.
The only correction I make is the excel version I have: 2016, not 2006.
Thanks for your interest.
Reply
#6
what openpyxl version do you use? Anyone else able to confirm OP problem?
I also use excel 2016 in my office
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I use openpyxl 3.0.0,
python 3.6.0.0 x64
excel 2016
windows 7
Reply
#8
I cannot test on windows till Monday, but with openpyxl 3.0.0 on Linux I cannot reproduce the error - it works as expected anyway you delete the content/cell A5. Sorry... Maybe someone else would be able to replicate it. IMHO OS or excel version would not matter. Maybe openpyxl version, but both 2.6.3 and 3.0.0 work as expected.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Ok.
To make things simpler, just type a letter in cell A1 and delete it. openpyxl finds one row in an empty sheet.
Reply
#10
I was about to suggest you report the problem if you think you have found a bug, but found that this was discussed on number of occasions, e.g.:
https://bitbucket.org/openpyxl/openpyxl/...han-actual

See author's comments. It looks at sheet dimensions property and obviously styles, fonts, cell format, etc. also count, not only cell content

By the way, your latest example with empty sheet works exactly as excel would do in VBA, i.e. last row on empty sheet is 1.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Combining Spreadsheets Py3_14 4 592 Feb-07-2024, 04:58 PM
Last Post: deanhystad
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,625 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 9,411 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,105 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,097 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 2,739 Jan-24-2021, 05:24 AM
Last Post: buran
  IDE with spreadsheets lewispgj 4 2,300 Mar-13-2020, 09:45 PM
Last Post: lewispgj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020