Python xlwt - accessing existing cell content, auto-adjust column width

后端 未结 6 1275
一向
一向 2020-11-29 03:17

I am trying to create an Excel workbook where I can auto-set, or auto-adjust the widths of the columns before saving the workbook.

I have been reading the Python-Ex

6条回答
  •  孤城傲影
    2020-11-29 03:40

    There is no automatic facility for this in xlwt. You have to follow the general pattern you describe, of keeping track of the max width as you're writing, and setting the column width at the end, sometime after you've seen all the data but before you've saved the workbook.

    Note that this is the cleanest and most efficient approach available when dealing with Excel files. If your notion of "after the data has already been written" means after you've already committed the cell values ("writing") but before actually saving the workbook, then the method described above is doing exactly this. If what you mean is after you've already saved the workbook, you want to read it again to get the max widths, and then save it again with new column widths, this will be much slower, and will involve using both xlwt and xlrd (and possibly xlutils as well). Also note that when you are using the genuine Microsoft Excel, there is no notion of "updating" a file. It may seem like that from a user point of view, but what is happening behind the scenes is that every time you do a save, Excel blows away the existing file and writes a brand new one from scratch.

提交回复
热议问题