Search code examples
pythonxlrdxlwt

Obtain cell value after xlrd and xlwt


After reading in a xls file using xlrd, I add a new sheet, add some data to that new sheet. Lets say the new sheet is called newSheet and I write the value 1 to A1 in. How can I access the value in newSheet in cell A1 later in the program? What I've gathered is I have to save the modified xls after xlwt and then do xlrd again. Is that true?


Solution

  • It's true, after you edit the xlsx file. You can save and then access it again to get the value. But I don't think you need to close it right after you insert the new value. Since the edits you did with the xlsx will be cached by xlsx. You can use that value right after you insert it, but you do need to save the sheet later if you want the change to remain after you close that xlsx file.

    This code will print out the A1 value.

    import xlrd
    
    
    book = xlrd.open_workbook("test.xls")
    sheet = book.sheet_by_index(0)
    a1 = sheet.cell_value(rowx=0, colx=0)
    print a1