简体   繁体   中英

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)?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM