Excel spreadsheet word search and full line return containing that word in PYTHON

5

Hello, is everything good? I am a beginner in Python and would like to run a script that given a spreadsheet in excel, the user could search for a word within that worksheet and if the word was found return "Found File and display all lines. For example, my worksheet:

NameAgeheightDanilo271.62John351.75Maria231.85Thalita251.80

TheuserlookingforDanilo,wouldliketheprogramtoreturn:FileFoundandNameAgeheightDanilo271.62

Followmycode:

fromxlrdimportopen_workbookworkbook=open_workbook("c:/Python34/test.xlsx")
sheet = workbook.sheet_by_index(0)
prompt = '>'
print ("Entre com o termo a ser buscado: ")
item = input(prompt)
found = False
for cell in sheet.col(0): 
     if cell.value == item:
       found = True
if found == True:
    print('Arquivo Encontrado', item) # tentei colocar (cell) mas dá errado
else:
    print('Arquivo não Encontrado')

Thank you

    
asked by anonymous 27.01.2016 / 14:50

1 answer

1

Particularly I like working with csv more than with xls. Just convert your xls to csv.

You can use regex or findall itself for this:

import re

data = open('seu_arquivo.csv')

texto = "Bem vindo ao python!"

resultado = re.findall(texto, data.read())

This will return you a list of the things you found without having to loop through repetition.

link

    
27.01.2016 / 16:24