我没有太多的代码:
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)
如您所见,我正在尝试将每个图片文件从“ .jpg”重命名为“ .png”,并且每个单元格都位于第一行(行A)。当我打印“ ws [fileCell] .value”时,它按预期工作,并将每个单元格值打印为[filename] .png,但是在excel中,没有任何变化。为什么是这样?
最佳答案
您需要保存文件:
filename = '' # This can be the same filename or a different one
wb.save(filename)
请参阅链接到here的文档中的“保存到文件”。