1.5 xlwt 自适应列宽

import xlwt
import copy

'''
利用python的xlwt模块自适应列宽写入excel
'''

# 获取每列所占用的最大列宽
def get_max_col(max_list):
    line_list = []
    # i表示行,j代表列
    for j in range(len(max_list[0])):
        line_num = []
        for i in range(len(max_list)):
            line_num.append(max_list[i][j])  # 将每列的宽度存入line_num
        line_list.append(max(line_num))  # 将每列最大宽度存入line_list
    return line_list

def write_excel():
    row_num = 0  # 记录写入行数
    col_list = []  # 记录每行宽度

    # 个人信息:姓名,性别,年龄,手机号,固定电话,邮箱
    data = [
                ['姓名','性别','年龄','手机号','固定电话','邮箱'],
                ['厘清','女','31','18745214693','0104784125','5412546qq.com'],
                ['张三','男','26','18245554693','010-4784125','无'],
                ['王武','男','19','13245266693','无','785992546qq.com'],
                ['熊大','男','16','无','010-4784125','115412546qq.com'],
                ['熊二','男','22','18745214693','010-4784125','3654126qq.com']
        ]

    # 创建一个Workbook对象
    book = xlwt.Workbook(encoding="utf-8",style_compression=0)

    # 创建一个sheet对象
    sheet = book.add_sheet('person_msg', cell_overwrite_ok=True)

    col_num = [0 for x in range(0, len(data))]
    # 写入数据
    for i in range(0, len(data)):
        for j in range(0, len(data[i])):
            sheet.write(row_num, j, data[i][j])
            col_num[j] = len(data[i][j].encode('gb18030')) # 计算每列值的大小
        col_list.append(copy.copy(col_num))  # 记录一行每列写入的长度
        row_num += 1

    # 获取每列最大宽度
    col_max_num = get_max_col(col_list)

    # 设置自适应列宽
    for i in range(0, len(col_max_num)):
        # 256*字符数得到excel列宽,为了不显得特别紧凑添加两个字符宽度
        sheet.col(i).width = 256 * (col_max_num[i] + 2)

    # 保存excel文件
    book.save('./办公自动化/files/person_msg.xls')

write_excel()