Home > Software engineering >  Python Pandas - how to write a string to a spcific cell via index without using the dataframe
Python Pandas - how to write a string to a spcific cell via index without using the dataframe

Time:01-16

I am trying to use pandas to write a value to a specific cell via index (1,1), in an xlsx file. Lets say I currently have an xlsx file:

A    B    C
1    2    3

How can I update 2 to another value without using the whole dataframe please? For the pupose of what I working on, I would like to specify the value as a string via index (1,1).

CodePudding user response:

You can just write to the cell using the openpxyl package:

Code:

import openpyxl

# Load the workbook
workbook = openpyxl.load_workbook("example.xlsx")

# Select the sheet you want to modify
worksheet = workbook["Sheet1"]

# Write to a specific cell
worksheet["B2"] = "Another Value"

workbook.save("example.xlsx")
  • Related