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

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


I just implemented a wrapper class that tracks the widths of items as you enter them. It seems to work pretty well.

import arial10class FitSheetWrapper(object):    """Try to fit columns to max size of any entry.    To use, wrap this around a worksheet returned from the     workbook's add_sheet method, like follows:        sheet = FitSheetWrapper(book.add_sheet(sheet_name))    The worksheet interface remains the same: this is a drop-in wrapper    for auto-sizing columns.    """    def __init__(self, sheet):        self.sheet = sheet        self.widths = dict()    def write(self, r, c, label='', *args, **kwargs):        self.sheet.write(r, c, label, *args, **kwargs)        width = arial10.fitwidth(label)        if width > self.widths.get(c, 0):            self.widths[c] = width            self.sheet.col(c).width = width    def __getattr__(self, attr):        return getattr(self.sheet, attr)

All the magic is in John Yeung's arial10 module. This has good widths for Arial 10, which is the default Excel font. If you want to write worksheets using other fonts, you'll need to change the fitwidth function, ideally taking into account the style argument passed to FitSheetWrapper.write.


If one is not interested in using another class (FitSheetWrapper), then this can be implemented using WorkSheet column Method.

work = xlwt.WorkBook()sheet = work.add_sheet('Sheet1')for row_index in range(0,max_row):   for column_index in range(0,max_col) :      cwidth = sheet.col(column_index).width      if (len(column_data)*367) > cwidth:            sheet.col(column_index).width = (len(column_data)*367) #(Modify column width to match biggest data in that column)      sheet.write(row_index,column_index,column_data,style)

Default value of width is 2962 units and excel points it to as 8.11 units. Hence i am multiplying 367 to length of data.

This is adapted from Kevins FitSheetWrapper.


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.