在xlsxwriter中自动适合列长度

时间:2018-08-31 14:18:32

标签: pandas xlsxwriter

我正在尝试下面的代码来查找列的最大长度并相应地调整其宽度,但出现错误提示:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xae'
in position 8: ordinal not in range(128)

下面是我的代码:

# Iterate through each column and set the width == the max
# length in that column A padding length of 2 is also added
for i, col in enumerate(df1.columns): 
    # Find length of column i    
    column_len = df1[col].astype(str).str.len().max()
    # Setting the length if the column header is larger than the
    # max column value length
    column_len = max(column_len, len(col)) + 2
    # Set the column length        
    worksheet.set_column(i, i, column_len)

0 个答案:

没有答案