[Python] How to save xls file in pandas 2.0+

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    Is there still a way to save an xls file in modern pandas (i.e. since they dropped xlwt)? For example, I can save an xlsx file like so:

    import pandas as pd


    def create_excel_files():
    # Create first sheet data
    df1 = pd.DataFrame(
    {
    "A": ["This is the first sentence.", "This is the second sentence."],
    "B": ["This is a third sentence.", "This is a fourth sentence."],
    }
    )

    # Create second sheet data
    df2 = pd.DataFrame(
    {
    "A": ["This is from sheet 2.", "Another sentence from sheet 2."],
    "B": ["More content here.", "Final sentence from sheet 2."],
    }
    )

    # Create XLSX file
    with pd.ExcelWriter("example.xlsx") as writer:
    df1.to_excel(writer, sheet_name="Sheet1", index=False)
    df2.to_excel(writer, sheet_name="Sheet2", index=False)
    print("Created example.xlsx")


    but if I try to do this:

    with pd.ExcelWriter("example.xls", engine='xlwt') as writer:
    df1.to_excel(writer, sheet_name="Sheet1", index=False)
    df2.to_excel(writer, sheet_name="Sheet2", index=False)
    print("Created example.xls")


    I get this error: ValueError: No engine for filetype: 'xls'

    If I try this:

    with pd.ExcelWriter("example.xls") as writer:
    df1.to_excel(writer, sheet_name="Sheet1", index=False)
    df2.to_excel(writer, sheet_name="Sheet2", index=False)
    print("Created example.xls")


    I get this error: ValueError: No engine for filetype: 'xls'

    Continue reading...

Compartilhe esta Página