[Python] How to add Excel Sheets to a new Excel File in Python

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am making a client manager using openpyxl, but i want to create sheets with different clients, for that, i have a function that receives an array "client_data" and generates a sheet, and the excel generator in another module. When I click "add user" it would create a new sheet for that client, but when I click "generate excel" it creates the excel with only the last sheet created, how can i save various sheets and add them in the final excel?

    client_sheet.py:

    seller_data = []

    def seller_to_df():
    filas = []
    for seller in seller_data:
    filas.append(seller.to_dict())
    df = pd.DataFrame(filas)
    data_table.rows = []
    data_table.update()
    return df, sheet_field.value if sheet_field.value else "Vendedores"


    main.py:

    `
    def convertToExcel(date):
    df, sheet_name = seller_to_df()
    with pd.ExcelWriter(f"REMATE {date}.xlsx", engine='openpyxl', mode="w") as writer:
    df.to_excel(writer, sheet_name=sheet_name, index=False)

    print("Excel generado")
    page.open(ft.SnackBar(
    ft.Text(f"Excel generado: REMATE {date}.xlsx"),
    duration=3000,
    bgcolor=ft.Colors.GREEN
    ))
    page.update()`

    Continue reading...

Compartilhe esta Página