[Python] Read content of Excel with multiple sheets with pandas and append new one sheet

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python script that reads content from JSON file and then write to excel file using pandas with that code and format:

    fichero_excel_salida = 'Estad.xlsx'
    nombre_pagina = 'Jornada_' + str(num_jornada)
    count = 0

    # Generamos los dataframe y Volcamos la info al fichero excel
    with pd.ExcelWriter(fichero_excel_salida, engine="openpyxl") as writer:

    for key,contenido_dict in dict_dict_excels.items():
    count = count +1
    key = pd.DataFrame(data=contenido_dict)
    if (count % 2) == 0:
    pass
    else:
    key = (key.T)
    print(key)

    if count == 1:
    key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=1)
    elif count == 2:
    key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=6, header=True, index=True)
    elif count == 3:
    key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=11)


    So the result of the code above, give me this with some format cells:

    A B C D E F G H I
    1
    2 10
    3 AA 2
    4 BB 3
    5
    6
    7 XX YY ZZ
    8 AA 5 3 6
    9 BB 6 2 6
    10
    11
    12 NUM MIN PTS
    13 John 3 24 6
    14 Dave 4 12 0
    15 Ko 5 30 2
    16 Dam 10 20 2
    17


    Now, I need to get new data from next week, and append this new data to new sheet in the same excel.

    What is the best form to do it?


    1. Reading the existing Excel, create new Excel file and put all data again (oldest and newest) using this code?

      dict_all_xlsx_file = pd.read_excel(fichero_excel_salida, sheet_name=None)


      How I can put the data with the same format as was in the previous excel file (with cell formats)?


    2. Append the new data of each week in the Excel?

      How I can do it, because using my Python script in Linux with pandas 2.2.3, all time create new Excel document and I lost old sheets.

    Continue reading...

Compartilhe esta Página