Import OS Import xlrd, sys # Input the excel file Filename = raw_input ('input the file name & path :') If not OS. path. isfile (Filename ): Raise NameError, "% s is not a valid filename" % Filename # Open the excel file Bk = xlrd. open_workbook (Filename) # Get the sheets number Shxrange = range (bk. nsheets) Print shxrange # Get the sheets name For x in shxrange: P = bk. sheets () [x]. name. encode ('utf-8 ') Print "Sheets Number (% s): % s" % (x, p. decode ('utf-8 ')) # Input your sheets name Sname = int (raw_input ('Choose the sheet number :')) Try: Sh = bk. sheets () [sname] Except t: Print "no this sheet" # Return None Nrows = sh. nrows Ncols = sh. ncols # Return the lines and col number Print "line: % d col: % d" % (nrows, ncols) # Input the check column Columnnum = int (raw_input ('which column you want to check pls input the num (the first colnumn num is 0 ):')) While columnnum + 1> ncols: Columnnum = int (raw_input ('your num is out of range, pls input again :')) # Input the searching string and column Testin = raw_input ('input the string :') # Find the cols and save to a txt Outputfilename = testin + '.txt' Outputfile = open (outputfilename, 'w ') # Find the rows which you want to select and write to a txt file For I in range (nrows ): Cell_value = sh. cell_value (I, columnnum) If testin in str (cell_value ): Outputs = sh. row_values (I) For tim in outputs: Outputfile. write ('% s' % (tim )) Outputfile. write ('% s' % (OS. linesep )) Outputfile. close () |