8

I am trying to write text to an excel I am following this post. This was working earlier but now it is not. I get:

Error:

line 122, in <module>
    worksheet.write(0, 11, 'HI')
AttributeError: 'Worksheet' object has no attribute 'write'

df1

A  E
c  d
c  D

Code:

writer = pd.ExcelWriter("C:\\33.xlsx")
df1.to_excel(writer, startrow=0, startcol=0, index = False)

worksheet = writer.sheets['Sheet1']
worksheet.write(0, 11, 'YO')
worksheet.write(1, 11, 'HI')

I have tried also:

import xlrd
import xlwt
from xlutils.copy import copy
import os.path
rb = xlrd.open_workbook('C:\\13.xlsx',formatting_info=True)
r_sheet = rb.sheet_by_index(0) 
wb = copy(rb) 
sheet = wb.get_sheet(0) 
sheet.write(5,2,"string")
wb.save('C:\\13.xlsx')

I get:

    with open(filename, "rb") as f:
OSError: [Errno 22] Invalid argument: 'C:\\13.xlsx"'

How do I fix AttributeError: 'Worksheet' object has no attribute 'write'

1 Answer 1

18

The reason it gives: AttributeError: 'Worksheet' object has no attribute 'write'

Is because I realised I have not installed xlsxwriter on this pc.

pip install xlsxwriter

Now it works.

Sign up to request clarification or add additional context in comments.

1 Comment

If you explicitly want to use XlsxWriter then you should probably add engine='xlsxwriter' to pd.ExcelWriter() otherwise Pandas could substitute OpenPyXL if that is available and XlsxWriter is not. Hence the missing method error. If you aren't using XlsxWriter specific methods then it doesn't matter which engine is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.