pythonopenpyxlxlrdxlwtxlutils

Add int value in a cell of excel file from python code


My excel file contains multiple sheets, I am trying to add value(int:5) in cell("C5") in sheet("sheet_1") of excel file("testexcel.xlsx"). I tried many ways, but none of the functions are able to edit the excel file. Excel File either gets corrupted or no changes are seen. Could you please suggest a way? Thank you! Note: https://www.geeksforgeeks.org/change-value-in-excel-using-python/ tried this methods


Solution

  • As suggested by Charlie Clark, if you want to use openpyxl, you can use the below code...

    import openpyxl
    wb = openpyxl.load_workbook("testexcel.xlsx")
    ws = wb["sheet_1"]
    val = 5 # Giving it separately in case you are getting this from another part of your program
    ws.cell(row=5, column=3).value = val #column C is the 3rd column
    wb.save("testexcel.xlsx")