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

776 views
Skip to first unread message

beatriz lopez

unread,
Jun 25, 2023, 6:50:24 PM6/25/23
to openpyxl-users
Hi,

I've already import the LAST VERSION of openpyxl to use it in my Django project.
But I keep getting the same error:
AttributeError: 'Worksheet' object has no attribute 'save'
It's very strange because I have used this library in another python project using Jupiter Notebook but now using Spyder in a django project it doesn't work.
Could anyone help me please?

This is my django view:

class Report_FB(TemplateView):
    def get(self, request, *args, **kwargs):
        query = ficha_FB.objects.all()
        wb = Workbook()
        ws = wb.active
        ws['B1'] = 'REPORTE'
        ws.merge_cells('B1:E1')
        ws['B3'] = 'MEDIDA'
        ws['C3'] = 'AÑO'
        ws['D3'] = 'PERIODO'
        ws['E3'] = 'CORREO'
        cont=4
        for q in query:
            ws.cell(row = cont, column = 3).value = q.año
            ws.cell(row = cont, column = 3).value = q.periodo
            ws.cell(row = cont, column = 3).value = q.correo
            cont +=1
       
        nombre_archivo = "Fuenlabrada_BienestarSocial.xlsx"
        response = HttpResponse(content_type = "application/ms-excel")
        contenido = "attachment; filename = {0}".format(nombre_archivo)
        response["Content-Disposition"] = contenido
        ws.template = False
        ws.save(response)
        return response




Cuatro Jones

unread,
Jun 25, 2023, 7:35:50 PM6/25/23
to openpyx...@googlegroups.com
It's been a while since I played around in Openpyxl, but if memory serves me correctly, the save method is tied to the 'Workbook' class, not the Worksheet class. 

--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openpyxl-users/538cbc98-79cf-4623-95ad-8ec0c7074419n%40googlegroups.com.

beatriz lopez

unread,
Jun 26, 2023, 2:15:12 AM6/26/23
to openpyxl-users
Thank you so much!
I have changed ws.save(response) for wb.save(response) and it worked! Thank u!

Cuatro Jones

unread,
Jun 26, 2023, 9:40:55 AM6/26/23
to openpyx...@googlegroups.com
No problem, glad it worked out!

Reply all
Reply to author
Forward
0 new messages