-1

My excel files contains some specific values and one of the cells contains general description. Sometimes in the description there are some warning messages with certain keywords (e.g. high, low, increase, decrease, closure). Is there a way to search for these specific words in an excel cell (the words might be in a sentence;e.g. unexpected high volume) and get the cell location?

I tried the following code, but not working. (just minor update, instead of knowing the cell location, I want to copy the files which do not have the warning message keywords)

import glob
import os
import shutil
import xlrd
os.chdir("C:/Users/tsengineer/Desktop/New folder/Trial")
choice = raw_input("Specify the Year to Copy (e.g. 2012) and Press Enter ")
location = raw_input("Specify the location to Copy (e.g. C:\Users\tsengineer\Desktop) and Press Enter ")
notcopy = ("increase", "decrease", "high", "low", "Closure")
for file in glob.glob("*.xls"):
    if choice in file:
        book = xlrd.open_workbook(file)
        sheet = book.sheet_by_index(0)
        for row in range(sheet.nrows):
            for column in range(sheet.ncols):
                if notcopy in sheet.cell(row,column).value:  
                    continue
                else:
                    shutil.copy(file, location)
print "Copying Complete:"
Joy
  • 61
  • 2
  • 8
  • This question is too broad. What have you tried? – Alain Jul 14 '14 at 20:05
  • I can search for a particular cell value but was wondering how to search for a string in a cell value – Joy Jul 14 '14 at 20:15
  • Can you include the code you've used so that it's clear what API you're using? – Alain Jul 14 '14 at 20:17
  • I was using xlrd 0.9.3 package in python 2.6.5. I have included the code which I tried. Currently I am getting "TypeError: 'in ' requires string as left operand, not tuple" – Joy Jul 14 '14 at 20:21

1 Answers1

0

I'm not a Python expert, but it looks like you're using the keyword in incorrectly.

You're checking to see if an array exists in the cell value, which isn't what you want. You want to see if any of the items in the array are in the cell value.

You're probably looking for something more like:

if any(s in sheet.cell(row,column).value for s in notcopy):
Alain
  • 26,663
  • 20
  • 114
  • 184
  • Thanks Alain. But I am getting the error "Traceback (most recent call last): File "C:\Users\tsengineer\Desktop\New folder\!FindExcelFile_yearwise_copy.py", line 15, in if any(s in sheet.cell(row,column).value for s in notcopy): File "C:\Users\tsengineer\Desktop\New folder\!FindExcelFile_yearwise_copy.py", line 15, in if any(s in sheet.cell(row,column).value for s in notcopy): TypeError: argument of type 'float' is not iterable" – Joy Jul 14 '14 at 20:35
  • @Joydip see http://stackoverflow.com/questions/3271478/check-list-of-words-in-another-string, and try using the cell's `.text` instead of it's `.value` property, or converting the value to a string. – Alain Jul 14 '14 at 20:37