pythonxlrdxlwtxlutils

Xlrd, xlwt, and xlutils: how do they work together


I'm trying to write to an existing xls file and then iterate over rows of the same file until I find a blank.

I understand how to do this via code.

Now my question is, does xlrd "update" it's reference to the sheet? Or do I have to create a new xlrd workbook object via the open workbook function any time I write and then save to it via xlwt's write and save function (to read the new workbook)?


Solution

  • xlrd will load your worksheet file into memory at the time you call the open_workbook() method. Any changes made to the worksheet file after you call open_workbook() are not automatically reflected in the object in memory.

    If you take a look at the xlrd source code on Github, in particular the open_workbook() method of the book.py file, you'll see that it loads the contents of the worksheet into memory when the open_workbook() method is called. That means, if you call open_workbook(), then change the original file, the workbook in memory will not reflect those changes.

    This is also why, when you use the xlwt module, you must use the write() method - when you modify the contents of the worksheet that is in memory, the changes are not reflected in the original file, until you call write().