pythonpython-3.xexcelpython-requestsstyleframe

How to apply Styleframe to an excel file that has two sheets


I have an excel file that hVave two sheets. I want to open the excel and apply Style frame to two sheets. Usually when the excel have only one sheet I use this code:

sf=StyleFrame.read_excel("test.xlsx", read_style=True)
sf.apply_headers_style(Styler(bg_color='#FFA500',bold=True,font='Calibri',font_size=11), cols_to_style=['col1','col2','col3'])
sf.to_excel('Testing_{}.xlsx'.format(datetime.now().strftime("%Y-%m-%d %H%M")), row_to_add_filters=0,columns_and_rows_to_freeze='C2').save()

In the other hand, when the excel have to sheets, if u use this code it will only style the sheet1 and when saving it it will save only one sheet.

Any idea how can i modify this code to apply styleframe to two sheets and save it as one file?


Solution

  • Just like you would with pandas, use sheet_name:

    sf_sheet_a = StyleFrame.read_excel("test.xlsx", read_style=True, sheet_name='sheet_a')
    sf_sheet_b = StyleFrame.read_excel("test.xlsx", read_style=True, sheet_name='sheet_b')
    

    And when saving, use an ExcelWriter object (again, just like pandas's API):

    excel_writer = StyleFrame.ExcelWriter('output.xlsx')
    sf_sheet_a.to_excel(excel_writer, sheet_name='sheet_a')
    sf_sheet_b.to_excel(excel_writer, sheet_name='sheet_b')
    excel_writer.save()
    

    Next time you can refer to the docs.