xlsxwriter 用法 - SeanWu1977/Machine-Learning GitHub Wiki

xlsxwriter


header = workbook.add_format({'align': 'center'})
        header.set_bold()
        header.set_italic()
        header.set_underline()

worksheet.write(0, col_hdr, key, header)

    # 會調整欄位寬
    
    writer = pd.ExcelWriter(filename, engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1',index=False)
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']
#    worksheet.autofilter(0,0,df.shape[0],df.shape[1]-1) # 設定filter
    
    for i,c in enumerate(df.columns):
        col_max = max(df[c].astype(str).map(len))
        worksheet.set_column(i,i, round(1.5 * max(col_max, len(c))))
        


        cell_format1 = workbook.add_format({'bg_color': '#e8f7c5'})
        cell_format2 = workbook.add_format({'bg_color': '#dbdddc'})                                    
        worksheet.freeze_panes(1, 0)       
        worksheet.set_default_row(hide_unused_rows=True)
        data_format_org0 = workbook.add_format({'font_color': '#FF0000'})
        data_format_org1 = workbook.add_format({'font_color': '#FF8800'})
        worksheet.set_column('B:B', None, cell_format1)
        worksheet.set_column('I:I', None, cell_format2)                                       
        fstring = 'data_format_org0'
        worksheet.set_row(row + 1, cell_format=eval(fstring))                

    writer.save()