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

后端 未结 6 1274
一向
一向 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.

提交回复
热议问题