pandas dataframe not posting correctly to excel

I've an excel sheet "Calcs" with 1 column name "old". I'm trying to add new column "new" with a fixed value of "1" to existing sheet "Calcs" and am using below code which is resulting 2 issues.

  1. it's not updating existing sheet rather it's creating new sheet called "Calcs1".

  2. After code is executed and while opening excel file, getting this error. (no such error while opening file before execution of the code).

    We found a problem with some content in 'test1.xlsx'. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click Yes.

Appreciate any help

    import pandas as pd
    from openpyxl import load_workbook
    
    file = r"C:\test1.xlsx"
    df2 = pd.read_excel(file, sheet_name = 'Calcs')
    df2["new"] = "1"
    
    book = load_workbook(file)
    writer = pd.ExcelWriter(file, engine = 'openpyxl')
    writer.book = book
    df2.to_excel(writer, sheet_name = 'Calcs')
    writer.save()
    
    writer.close()

Output with issues



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation