有些样式没有导出到excel,而有些样式是从my pandas dataframe导出的

2ul0zpep  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(303)

我有一个应用了样式的多索引 Dataframe 。

df = (df.style.format('{:,.0f%}', na_rep='-',subset=[col for col in df.columns if col[0] in ('A','B','C')])).apply(highlight_cells, axis = None)\
.set_table_styles([{'selector':'th','props':[('border','1px solid black'),('font-size','8pt')]},
                   {'selector':'td','props':[('border-left','1px dotted black')]}])

导出到excel时,某些样式会出现,但某些样式不会出现,如:
仅获取标题的边框,而不获取完整列的边框,
“%”符号也没有导出。
我也尝试过使用xlsxwriter,但也不起作用。写入导出到excel的代码:

def func(df,title="Download excel file",filename="ABC.xlsx"):
    writer = ExcelWriter('data.xlsx', engine='xlsxwriter')
    xls=df.to_excel(writer,'sheet1') 

    workbook = writer.book
    worksheet = writer.sheets['sheet1']
    percent_fmt = workbook.add_format({'num_format':'0.0%'})
    worksheet.set_column('Z:Z',percent_fmt)
    writer.save()

    def file_to_base64(file):
       with open(file, "rb") as image_file:
         encoded_string = base64.b64encode(image_file.read())
         return encoded_string.decode() 
    payload=file_to_base64('data.xlsx')
    html = "<a href="data:text/xls;base64,{payload}" download="{filename}" target="_blank">{title} 
    </a>"
    html = html.format(payload=payload,title=title,filename=filename)
    return HTML(html)

有人能帮忙吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题