在保持格式的同时根据 pandas 数据框中的列值导出许多 excel 文件?

Exporting many excel files based on column value in pandas dataframe while maintaining formatting?

我有一个数据框,我想根据“名称”列中的唯一字符串将其解析为单个文件。我可以用一个简单的函数来做到这一点:

f= lambda x: x.to_excel(os.getcwd()+'\{}.xlsx'.format(x.name), index= False)
df.groupby('names').apply(f) 

需要注意的是,我想用条件格式写出这些文件。我已经能够在以下块中使用 ExcelWriter 实现所需的格式:

writer= pd.ExcelWriter('Test.xlsx', engine='xlsxwriter' )
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook= writer.book
worksheet= writer.sheets['Sheet1']
format1= workbook.add_format({'font_color':'#ec5a24'})
format2= workbook.add_format({'bg_color':'#4b4b4b','font_color':'#ffde00'})
format3= workbook.add_format({'font_color':'#86b05b'})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "High"', 'format':format1})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Med"', 'format':format2})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Normal"', 'format':format3})
workbook.close()
writer.save()

是否有一种方法可以让我将这两者结合起来,以便我的输出是许多文件(列名称中的每个唯一名称一个),这些文件在 excel 文件中具有所需的条件格式?

每个请求的新代码:

def new_func():
    f= lambda x: x.to_excel(os.getcwd()+'\{}.xlsx'.format(x.name), index= False)
    writer= pd.ExcelWriter(x.name, engine='xlsxwriter' )
    df.to_excel(writer, sheet_name='Sheet1', index=False)
    workbook= writer.book
    worksheet= writer.sheets['Sheet1']
    format1= workbook.add_format({'font_color':'#ec5a24'})
    format2= workbook.add_format({'bg_color':'#4b4b4b','font_color':'#ffde00'})
    format3= workbook.add_format({'font_color':'#86b05b'})
    worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "High"', 'format':format1})
    worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Med"', 'format':format2})
    worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Low"', 'format':format3})
    workbook.close()
    writer.save()
df.groupby('names').apply(new_func())

对您的代码进行一些修改。可能需要一些调试才能使其正确。很难说,因为我没有要测试的数据。但我认为你走在正确的轨道上

def new_func(x): # add x as and object to be passed to the function
    # remove f= lambda x: from line below. you aren't calling a lambda function any longer
    x.to_excel(os.getcwd()+'\{}.xlsx'.format(x.name), index= False)
    writer= pd.ExcelWriter(x.name, engine='xlsxwriter' )
    # the df below should be x, I think???
    x.to_excel(writer, sheet_name='Sheet1', index=False)
    workbook= writer.book
    worksheet= writer.sheets['Sheet1']
    format1= workbook.add_format({'font_color':'#ec5a24'})
    format2= workbook.add_format({'bg_color':'#4b4b4b','font_color':'#ffde00'})
    format3= workbook.add_format({'font_color':'#86b05b'})
    worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "High"', 'format':format1})
    worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Med"', 'format':format2})
    worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Low"', 'format':format3})
    workbook.close()
    writer.save()
df.groupby('names').apply(new_func) #removed () after new_func. I don't think you need it with the current syntax

这是对我有用的解决方案(我也添加了 header 格式),使用了 johathan 的大量帮助:

def new_func(x):
        writer= pd.ExcelWriter(os.getcwd()+'\{}.xlsx'.format(x.name), engine='xlsxwriter' )
        x.to_excel(writer, sheet_name='Sheet1', index=False)
        workbook= writer.book
        worksheet= writer.sheets['Sheet1']
        format1= workbook.add_format({'font_color':'#ec5a24'})
        format2= workbook.add_format({'bg_color':'#4b4b4b','font_color':'#ffde00'})
        format3= workbook.add_format({'font_color':'#86b05b'})
        header= workbook.add_format({'font_size':14, 'bold': True, 'font_color':'#3582c0'})
        worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "High"', 'format':format1})
        worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Med"', 'format':format2})
        worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Low"', 'format':format3})
        pandas.io.formats.excel.ExcelFormatter.header_style = None
        worksheet.set_row(0, None, header)
        workbook.close()
        writer.save()
    
       
df.groupby('name').apply(new_func)