I have a script in Python that uses xlwings to open up an Excel file, and read and process the values of a certain column row by row. Here is the for statement:
for row in range(2, rownum):
I would like to repeat this function over every row in the sheet that actually contains something. It starts at 2 and ends at ‘rownum’. My question is how to automatically count the number of rows and pass that value to ‘rownum’. I’m sure xlwings has a way to do this, but I can’t figure it out- perhaps the Autofit tool?
Thanks for the help!
asked Jul 27, 2015 at 16:52
It’s all the API Documentation
If you’re only looking for the number of rows, you can obtain the total number of row in your array/table by using the current_region
property of your range and then getting the address of the last cell of this range: (It works only if your range is contiguous — no empty rows/columns inside of it)
rownum = Range('A1').current_region.last_cell.row
Alternatively, you can use table
instead of current_region
, the range will just be a bit different.
Once you have that, you can just loop through the rows:
for i in range(1, rownum + 1): # The indexing starts at 1
Range((i, 1)) = ... # Will select cell 'Ai'
But as mentioned in other answers, this multiplies the calls between app, which will be considerably slower. Better import the range, modify it and export it back to Excel.
answered Aug 13, 2015 at 21:29
ursanursan
2,1482 gold badges14 silver badges21 bronze badges
Unless I’ve missed something while reading their API documentation it doesn’t seem possible. You might need to use other libraries, for example pandas
:
import pandas as pd
df = pd.read_excel(excel_file_path, sheetname="Sheet1")
print len(df)
If you don’t want to use another library just for that, you can do it the hard and ugly way:
last_row = 0
while True:
if cell_value is not None: # replace cell_value with however
# xlwings accesses a cell's value
last_row += 1
else:
break
print last_row
answered Jul 27, 2015 at 17:16
DeepSpaceDeepSpace
77.6k11 gold badges106 silver badges151 bronze badges
2
With xlwings, you would read in the Range first, then iterate over it:
rng = Range((startrow, startcol), (rownum, colnum)).value
for row in rng:
...
Then at the end, write the result back:
Range((startrow, startcol)).value = result_rng
This way you minimize the cross-application calls which are slow.
You might also want to use Range.table
.
answered Jul 27, 2015 at 22:21
Felix ZumsteinFelix Zumstein
6,6071 gold badge28 silver badges62 bronze badges
I had to make a counter because I am automating a bunch of things that taken from excel and filled onto different websites. This is just the «prototype» that I came up with just to do it make sure I could do it.
wb = xw.Book(r'C:UsersddDesktopTestbook.xlsm')
Dudsht = wb.sheets['Dud']
lastcell = Dudsht.range(1,1).end('down').row #this just does ctrl+shift+down
print(lastcell) #just so you know how many rows you have. Mine was 30.
x = 2
for i in range(x, lastcell+1): #range of 2 to 30
Dudsht.cells(i,2).value = 'y' #enters 'y' triggering formulas
if Dudsht.cells(i,1).value == 'ERROR':
Dudsht.cells(i,1).api.EntireRow.Interior.ColorIndex = 2
continue #if there is an error it will hightlight and skip an item
time.sleep(.5) #this was just so I could see visually
Dudsht.cells(i,2).value = 'x'
print('Item' + str(i) + ' Complete') #Item1 Complete
time.sleep(.5)
Dudsht.cells(i,1).api.EntireRow.Interior.ColorIndex = 3 #highlights completed item
answered Mar 9, 2018 at 19:30
NoctsolNoctsol
4581 gold badge8 silver badges12 bronze badges
If there is no blank row, you can just use this:
len(Range('A1').vertical)
answered Feb 23, 2016 at 7:21
You don’t need to know how many rows in the sheet.
import xlwings as xw
wb = xw.Book('20180301.xlsm')
sh = wb.sheets['RowData']
rownum = 2
while (sh.range('A'+str(rownum)).value != None):
value = sh.range('A'+str(rownum)).value
print(str(value))
rownum += 1
This will print out all data in column A.
answered Apr 27, 2018 at 4:20
For counting rows in a column with empty cells in between:
import xlwings as xw
wb = xw.Book(loc)
sheet = wb.sheets['sheetname']
counter = 0
rownum = 1
while (rownum >= 1):
if sheet.range('A'+str(rownum)).value !=None:
counter += 1
elif sheet.range('A'+str(rownum)).value == None and sheet.range('A'+str(rownum+1)).value != None:
counter += 1
elif sheet.range('A'+str(rownum)).value == None and sheet.range('A'+str(rownum+1)).value == None:
counter += 1
break
rownum += 1
answered Mar 14, 2022 at 15:56
1
The solution suggested in this answer has been deprecated, and might no longer work.
Taking a look at the source code of OpenPyXL (IterableWorksheet) I’ve figured out how to get the column and row count from an iterator worksheet:
wb = load_workbook(path, use_iterators=True)
sheet = wb.worksheets[0]
row_count = sheet.get_highest_row() - 1
column_count = letter_to_index(sheet.get_highest_column()) + 1
IterableWorksheet.get_highest_column
returns a string with the column letter that you can see in Excel, e.g. «A», «B», «C» etc. Therefore I’ve also written a function to translate the column letter to a zero based index:
def letter_to_index(letter):
"""Converts a column letter, e.g. "A", "B", "AA", "BC" etc. to a zero based
column index.
A becomes 0, B becomes 1, Z becomes 25, AA becomes 26 etc.
Args:
letter (str): The column index letter.
Returns:
The column index as an integer.
"""
letter = letter.upper()
result = 0
for index, char in enumerate(reversed(letter)):
# Get the ASCII number of the letter and subtract 64 so that A
# corresponds to 1.
num = ord(char) - 64
# Multiply the number with 26 to the power of `index` to get the correct
# value of the letter based on it's index in the string.
final_num = (26 ** index) * num
result += final_num
# Subtract 1 from the result to make it zero-based before returning.
return result - 1
I still haven’t figured out how to get the column sizes though, so I’ve decided to use a fixed-width font and automatically scaled columns in my application.
Is there any method to get the number of rows and columns present in .xlsx sheet using openpyxl ?
In xlrd,
sheet.ncols
sheet.nrows
would give the column and row count.
Is there any such method in openpyxl ?
asked Feb 15, 2016 at 11:36
1
Given a variable sheet
, determining the number of rows and columns can be done in one of the following ways:
Version ~= 3.0.5 Syntax
rows = sheet.max_rows
columns = sheet.max_column
Version 1.x.x Syntax
rows = sheet.nrows
columns = sheet.ncols
Version 0.x.x Syntax
rows = sheet.max_row
columns = sheet.max_column
MacGyver
18.2k38 gold badges159 silver badges242 bronze badges
answered Feb 15, 2016 at 11:42
Alexander CraggsAlexander Craggs
7,5244 gold badges23 silver badges44 bronze badges
7
Worksheet has these methods: ‘dim_colmax’, ‘dim_colmin’, ‘dim_rowmax’, ‘dim_rowmin’
Below is a small example:
import pandas as pd
writer = pd.ExcelWriter("some_excel.xlsx", engine='xlsxwriter')
workbook = writer.book
worksheet = writer.sheets[RESULTS_SHEET_NAME]
last_row = worksheet.dim_rowmax
answered Jul 26, 2020 at 14:46
1
this is the logic
number_of_rows = sheet_obj.max_row
last_row_index_with_data = 0
while True:
if sheet_obj.cell(number_of_rows, 3).value != None:
last_row_index_with_data = number_of_rows
break
else:
number_of_rows -= 1
answered Nov 5, 2020 at 12:31
Building upon Dani’s solution and not having enough reputation to comment in there. I edited the code by adding a manual piece of control to reduce the time consumed on searching
## iteration to find the last row with values in it
nrows = ws.max_row
if nrows > 1000:
nrows = 1000
lastrow = 0
while True:
if ws.cell(nrows, 3).value != None:
lastrow = nrows
break
else:
nrows -= 1
answered Feb 16, 2021 at 15:45
AshGAshG
112 bronze badges
A solution using Pandas to get all sheets row and column counts. It uses df.shape
to get the counts.
import pandas as pd
xl = pd.ExcelFile('file.xlsx')
sheetnames = xl.sheet_names # get sheetnames
for sheet in sheetnames:
df = xl.parse(sheet)
dimensions = df.shape
print('sheetname', ' --> ', sheet)
print(f'row count on "{sheet}" is {dimensions[0]}')
print(f'column count on "{sheet}" is {dimensions[1]}')
print('-----------------------------')
answered Jul 21, 2021 at 13:29
CamCam
1,17310 silver badges21 bronze badges
Try
import xlrd
location = ("Filelocationfilename.xlsx")
wb = xlrd.open_workbook(location)
s1 = wb.sheet_by_index(0)
s1.cell_value(0,0) #initializing cell from the cell position
print(" No. of rows: ", s1.nrows)
print(" No. of columns: ", s1.ncols)
answered Nov 23, 2021 at 6:18
Merrin KMerrin K
1,5401 gold badge15 silver badges26 bronze badges
Who doesn’t know about Excel Files these days? Whether you tabulate the score of an exam of a class in schools or you make a bill and store information of a payment you have or analyzing the data for a particular area; Excel Files are a one-step solution for making these.
Well, in the programming world also, excel used for a number of purposes. For example, datasets for machine learning can be in the form of excel files. In Python, we can work with excel in many ways.
Python program to count the no of Rows & Columns of an excel file
In this article, I’m here to show how to count the number of rows and columns from an excel file with the help of Python. Before programming in Python, I must recommend installing xlrd package in Python as this library is used for reading & formatting data from excel files having extensions .xls or .xlsx.
Also, read:
- Read CSV file in Python using Pandas Library
Just follow the steps given below:
Install xlrd package
To install xlrd using Windows, open Command Prompt and type the command given below:
python -m pip install -U xlrd
or if you’re using Linux or Raspberry Pi then type the command on your respective terminals:
sudo pip install xlrd
Creating an Excel File
Now we create an excel file from Microsoft Excel consisting of rows and columns given below:
Write our Python Program
After creating the file, here the main part comes: we write the following code. For a better understanding of the code, I used comment lines to clarify the steps.
import xlrd as xl #Import xlrd package loc = ("Filelocationfilename.xlsx") #Giving the location of the file wb = xl.open_workbook(loc) #opening & reading the excel file s1 = wb.sheet_by_index(0) #extracting the worksheet s1.cell_value(0,0) #initializing cell from the excel file mentioned through the cell position print("No. of rows:", s1.nrows) #Counting & Printing thenumber of rows & columns respectively print("No. of columns:", s1.ncols)
Output:
No. of rows: 6 No. of columns: 4
Just follow the comment lines in our code to understand each and every step.
Решение, предложенное в этом ответе, устарело и может перестать работать.
Взглянув на исходный код OpenPyXL (IterableWorksheet) Я выяснил, как получить количество столбцов и строк из лист итератора:
wb = load_workbook(path, use_iterators=True)
sheet = wb.worksheets[0]
row_count = sheet.get_highest_row() - 1
column_count = letter_to_index(sheet.get_highest_column()) + 1
IterableWorksheet.get_highest_column
возвращает строку с буквой столбца, которую вы можете видеть в Excel, например. «A», «B», «C» и т.д. Поэтому я также написал функцию для перевода буквы столбца к индексу на основе нуля:
def letter_to_index(letter):
"""Converts a column letter, e.g. "A", "B", "AA", "BC" etc. to a zero based
column index.
A becomes 0, B becomes 1, Z becomes 25, AA becomes 26 etc.
Args:
letter (str): The column index letter.
Returns:
The column index as an integer.
"""
letter = letter.upper()
result = 0
for index, char in enumerate(reversed(letter)):
# Get the ASCII number of the letter and subtract 64 so that A
# corresponds to 1.
num = ord(char) - 64
# Multiply the number with 26 to the power of `index` to get the correct
# value of the letter based on it index in the string.
final_num = (26 ** index) * num
result += final_num
# Subtract 1 from the result to make it zero-based before returning.
return result - 1
Я все еще не понял, как получить размеры столбцов, поэтому я решил использовать шрифт с фиксированной шириной и автоматически масштабированные столбцы в моем приложении.