Python Forum

Full Version: find cell value with matching regular expression of a row in excel file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Python -- 3.5 pip 8.1.2 openpyxl centos 7

I am trying to read an excel file using above specifications. So i loop through all rows in an excel file and find a cell value matching regular expression and if that cell value is found then I should check the next immediate row is empty and if its empty I should print the name of the sheet.

#!/usr/bin/python3.5

import os, sys, re
from re import *
from openpyxl import *

f = "/root/python/countsheets/sample.xlsx"
print (f)
wb = load_workbook(f)
for sheet in wb:
       print ("\n")
       print ("Checking Sheet:", sheet)
# looping through all rows and first column
   for row in sheet.iter_rows('A{}:A{}'.format(sheet.min_row,sheet.max_row)):
               for col in sheet.iter_cols(min_col=1,max_col=1):
                       for cell in row:
# printing all cell values from all rows and first column
                               cv = cell.value
# matching regular expression with cell value
                               m = re.search(r"^yang*type*", str(cv))
                               print (cell.row,cell.column)
                               if m == cv:
                                       print (cell.coordinate)
                               else:
                                       print ("Not found")
How can I get into next row after finding cell value and check if it is empty?
Hi hruday,

If I understand correctly, you know the row number of the current row,
Quote:print (cell.row,cell.column)
 so can you not run the if test on the next row by using the row value of cell.row and adding a 1? so this would be "cell.row +1"

Or have I missed the point, let me  know.

Good Luck

Bass
Hi Bass, thanks for the reply..

if i increment the row number then how can i loop through row values for that row number?
Unless I've missed something, you would loop through the row in exactly the same manner, the difference being that when you access the new row (the one below) then you add a "+ 1" after the variable name. It depends on how your code is set up, but rather than increase the value of the row by (row += 1) which will change the value itself, I would suffix the variable row with a + 1. This leaves the actual value the same but allows you to manipulate and scan the row below or (5 rows below if you use a + 5 etc.!)

Does this makes sense? If not then maybe you could update the code, as far as you can and then we can look at it again?

Best,

Bass
While your at it, you might want to upgrade 'pip' to the latest version (currently 9.0.1)

from the command line, type and enter:

pip install --upgrade pip