How to Search In Excel Worksheet with Python

Working with Python and Excel together is really fun for a VBA Developer! It somehow shows you what all these years of coding with VBA have taught you! Today, I was thinking how to implement the .Find() method of searching in Excel values.


At first, I have tought about going old school and simply loop through all the cells in a given Excel range, check their values and thus implement a “manual” search. For this, I have generated an Excel file with the xlswriter library and I have read it with the xlrd library. Both are quite easy to be used, if you are aware how the Excel object model is built.

This is the generated excel file from the first part of the code. (Does it remind something from here?):

This is what Python prints, which is actually quite nice:


SecondlyI remembered that I have actually learnt programming in Python some years ago and this is a programming blog, thus I should probably not code as a first year student (or so). Thus, I have decided to introduce at least one function. So, the function was named findCell and I have rewritten my code to this:

Now, it is a bit fancier, as the code could be executed with a click. On the previous one, I have written quit() , thus one should execute it from the console. Still, B10 is found:





Thirdly, I have read a comment from @ashleedawg, that one should be able to use the Excel API and thus use the Find() method from it. The whole programming becomes quite easy this way, using the xlwings library:

Thus, from an imput like this:

I was still able to get the A10:


That’s all folks! All the code is in GitHub here – Python Find.

Tagged with: , , ,