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

后端 未结 6 1246
一向
一向 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:18

    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.

    0 讨论(0)
  • 2020-11-29 03:29

    FitSheetWrapper should have a little modify with xlwt3 in 3.3.4

    line 19:

    change:

    width = arial10.fitwidth(label)
    

    to:

    width = int(arial10.fitwidth(label))  
    

    reason: \Python\3.3.3\Lib\site-packages\xlwt3\biffrecords.py

    1624 def __init__(self, first_col, last_col, width, xf_index, options):
    1625        self._rec_data = pack('<6H', first_col, last_col, width, xf_index, options, 0)
    

    width must be integer.

    0 讨论(0)
  • 2020-11-29 03:31

    i use this method:

    wb = Workbook()
    ws = wb.add_sheet('Sheet1')
    columnwidth = {}
    row = 0
    for rowdata in data:
        column = 0
        for colomndata in rowdata:
            if column in columnwidth:
                if len(colomndata) > columnwidth[column]:
                    columnwidth[column] = len(colomndata)
            else:
                columnwidth[column] = len(colomndata)
            ws.write(row, column, colomndata, style0)
            column = column + 1
        row = row + 1
    for column, widthvalue in columnwidth.items():
        ws.col(column).width = (widthvalue + 4) * 367
    
    0 讨论(0)
  • 2020-11-29 03:35

    This may be a little late, but I created a method that does this for the whole sheet at once. It's quick and gets the job done. The extra cushion param. is only needed if you think that the 256 calculation won't be accurate (if you have longer text fields).

    from xlrd import *
    from xlwt import *
    
    def autoAdjustColumns(workbook, path, writerSheet, writerSheet_index, extraCushion):
        readerSheet = open_workbook(path).sheet_by_index(writerSheet_index)
        for row in range(readerSheet.nrows):
                for column in range(readerSheet.ncols):
                        thisCell = readerSheet.cell(row, column)
                        neededWidth = int((1 + len(str(thisCell.value))) * 256) 
                        if writerSheet.col(column).width < neededWidth:
                                writerSheet.col(column).width = neededWidth + extraCushion
        workbook.save(path)
    
    0 讨论(0)
  • 2020-11-29 03:39

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

    import arial10
    
    class 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题