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

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

提交回复
热议问题