Python Forum
find cell value with matching regular expression of a row in excel file
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find cell value with matching regular expression of a row in excel file
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.


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 ="^yang*type*", str(cv))
                               print (cell.row,cell.column)
                               if m == cv:
                                       print (cell.coordinate)
                                       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

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?


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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy column from one existing excel file to another file mkujawsk 0 216 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
Music XML File - cannot find the tag ateestructural 1 245 Apr-06-2021, 08:26 AM
Last Post: ibreeden
  Importing text file into excel spreadsheet with formatting david_dsmn 1 217 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 292 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 187 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Need help with saving output into an excel file Beyondfacts 4 360 Mar-22-2021, 11:51 AM
Last Post: jefsummers
  How to append a value to specific excel cell using openpyxl hobbyist 0 334 Mar-05-2021, 07:14 PM
Last Post: hobbyist
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 237 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Python Matplotlib: Create chart for every 4 columns in Excel file JaneTan 2 450 Feb-28-2021, 05:02 AM
Last Post: JaneTan
  Find specific file in an archive tester_V 8 525 Feb-13-2021, 06:13 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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