Dinosaur212
Dinosaur212

Reputation: 123

Why can't I change the cell value in excel using python and openpyxl?

I don't have very much code for this:

wb = openpyxl.load_workbook(r"C:\Users\julia\demographics.xlsx")
sheets = wb.sheetnames
ws = wb[sheets[0]]


for i in range(2, 26664):
    fileCell = "A" + str(i)
    currentFileName = str(ws[fileCell].value)
    startFile, ext = os.path.splitext(currentFileName)
    ext = ".png"
    ws[fileCell].value = startFile + ext
    print(ws[fileCell].value)

As you can see, I am trying to rename every picture file from ".jpg" to ".png" and each cell is located in the first row (row A). When I print "ws[fileCell].value", it works as expected and prints every cell value as [filename].png but in excel nothing changes. Why is this?

Upvotes: 1

Views: 3577

Answers (1)

Matt McCarthy
Matt McCarthy

Reputation: 61

You need to save the file:

filename = ''  #  This can be the same filename or a different one
wb.save(filename)

Please see Saving to a file in the documentation linked here.

Upvotes: 2

Related Questions