Search code examples
pythonpandasxlsxwriterworksheet

AttributeError: 'Worksheet' object has no attribute 'set_default_row'


I am getting an error that seems weird. Worksheet object does have set_default_row() function, in the docs. Not sure what I am missing here.

I got this code project from someone who made this and has been running for a long time. We are using different python versions. He's on 3.10 and I am on 3.9 and I don't see that to be any reason.

Error:

Traceback (most recent call last):
  File "C:\Users\ajoshi\my_folder\misc\quick tools\CI-TestRun-Report-Generator\FileProvider.py", line 31, in create
    worksheet.set_default_row(20)
AttributeError: 'Worksheet' object has no attribute 'set_default_row'

Code:

s = data.style.applymap(FileProvider.color_negative_red)
s.to_excel(writer, sheet_name=plan["name"], header=True, index=False)

workbook = writer.book
worksheet = writer.sheets[plan["name"]]
worksheet.set_default_row(20)
worksheet.set_row(0, 40)

Solution

  • The issue is that you are calling a xlsxwriter method but that, most probably, the module isn't installed so Pandas is defaulting to creating a openpyxl worksheet object which has different APIs and doesn't have that method. Try set up your Pandas xlsx writer like this:

    writer = pd.ExcelWriter('filename.xlsx', engine='xlsxwriter')
    

    If that fails then you need to install xlsxwriter.

    If you are already using engine='xlsxwriter' then the issue could be that you have a very old version installed that doesn't support the set_default_row() method. In which case you should upgrade xlsxwriter.