Home > Back-end >  Unable to write data into xlsx file in python using openpyxl
Unable to write data into xlsx file in python using openpyxl

Time:11-11

I am not sure what I am doing wrong here.It always throws an error. I want to achieve to write test data strings inside B4 cell in the existing excel file.

from openpyxl import load_workbook
template_wb = load_workbook(r'E:\test.xlsx')
template_ws= template_wb.sheetnames[0]
template_ws['B4']='test data'
template_wb.save()
template_wb.close()

Error:

template_ws['B4']='test data' TypeError: 'str' object does not support item assignment

CodePudding user response:

It must be worksheets not the sheetnames:

from openpyxl import load_workbook

template_wb = load_workbook(r'E:\test.xlsx')
template_ws= template_wb.worksheets[0]
template_ws['B4']='test data'
template_wb.save("E:\test.xlsx")
template_wb.close()
  • Related