1

I created this topic ( Delete lines found in file with many lines ) and they suggested using the package "xlrd". I used and got interact with the file, but could not compare the contents of the cell with some string. Here's my code:

import xlrd
arquivo = xlrd.open_workbook('/media/IRREMOVIVEL/arquivo.xls',)
planilha = arquivo.sheet_by_index(0)


def lerPlanilha():
    for i in range(planilha.ncols):
        if (planilha.cell(8,9) == "2010"):
                print 'it works =>'
                break
    else:
            print 'not works'
                break    

lerPlanilha()

But I got error: not works

Sorry for duplicate, maybe, and bad english.

Community
  • 1
  • 1
Filipe Manuel
  • 967
  • 2
  • 14
  • 33
  • Maybe I expressed myself wrong, I want to compare a string with a given cell, if the data is equal, it will be deleted. – Filipe Manuel Aug 01 '12 at 19:32

1 Answers1

0

xlrd.sheet.Sheet.cell method returns xlrd.sheet.Cell instance, which represents cell with value stored in it's value attribute. So something like sheet.cell(x,y).value should work.

And about deleting - you can't modify document using xlrd, you should use xlwt for writing excel, and xlutils for reading, modifying and writing document. A little intense in googling gives you something like http://www.python-excel.org/

Victor Gavro
  • 1,347
  • 9
  • 13