Python Forum
module 'openpyxl.workbook' has no attribute 'active'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module 'openpyxl.workbook' has no attribute 'active'
#1
Hello team,

I am facing with the following issue when trying to download info to excel file, after clicking on hyperlink to download file the browser shows the issue: module 'openpyxl.workbook' has no attribute 'active'

but as per my understanding I have activated the workbook



this is the view/class on views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Member
#from .resources import Memberexcel
from openpyxl import workbook
from django.views.generic import TemplateView


from .serializers import Member_Model_Serializer
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.authentication import SessionAuthentication


class MemberExcel(TemplateView):
  def get(self, request, *args, **kwars):
    memberset = Member.objects.all()
    wb = workbook
    ws = wb.active
    
    ws['B1'] = 'Members Report'

    ws.merge.cells('B1:E1')

    ws['B3'] = 'FirstName'
    ws['C3'] = 'LastName'
    ws['D3'] = 'Phone'
    ws['D3'] = 'joined date'

    count = 4

    for member in memberset:
      ws.cell(row = count, column = 2).value = member.firstname
      ws.cell(row = count, column = 3).value = member.lastname
      ws.cell(row = count, column = 4).value = member.phone
      ws.cell(row = count, column = 5).value = member.joined_date
      count += 1

    file_name = 'Members Report.xlsx'

    response = HttpResponse(content_type='application/ms-excel')
    content = "attachment; filename = {0}".format(file_name)
    response['Content-Disposition'] = content
    wb.save(response)

    return response


###here the URL.py

from django.contrib import admin
from django.urls import path, include
# from rest_framework.routers import DefaultRouter
from . import views
from .views import MemberExcel 





urlpatterns = [
    path('', views.main, name='main'),
    path('members/', views.members, name='members'),
    path('members/details/<slug:slug>', views.details, name='details'),
    path('testing/', views.testing, name='testing'),
    path('admin/', admin.site.urls),
    path('memberst/', views.memberst, name='memberst'),
    path('memberst/MemberExcel/', MemberExcel.as_view(), name='MemberExcel'),

    

]
for reference this is the line in my html template that calls the download:
Quote: <p><a href="MemberExcel/">Export to excel</a></p>


this is the error shown on the pyton terminal

Quote:Traceback (most recent call last):
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\views\generic\base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\views\generic\base.py", line 143, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\my_tennis_club\members\views.py", line 47, in get
ws = wb.active
AttributeError: module 'openpyxl.workbook' has no attribute 'active'

Thanks in advance for help provided
Reply
#2
>>> from openpyxl import Workbook  # Import Workbook, not workbook
>>> 
>>> wb = Workbook()
>>> ws = wb.active
>>> ws['B1'] = 'Members Report'
>>> ....
lsaavedra21 likes this post
Reply
#3
The wording of the error provides a hint:
Quote:AttributeError: module 'openpyxl.workbook' has no attribute 'active'
The MODULE openpyxl.workbook has no attribute 'active'. This means that wb in your code is not a Workbook object, it is the openpyxl.workbook module. You need to create a Workbook object like in @snippsat's example.
lsaavedra21 likes this post
Reply
#4
(Oct-30-2024, 02:51 PM)snippsat Wrote:
>>> from openpyxl import Workbook  # Import Workbook, not workbook
>>> 
>>> wb = Workbook()
>>> ws = wb.active
>>> ws['B1'] = 'Members Report'
>>> ....

Thanks buddy, I added the () in t he line code you told me.
So the View/class now looks as follow, but now I am getting the error I show you at the end of this comment;

class MemberExcel(TemplateView):
  def get(self, request, *args, **kwargs):
    memberset = Member.objects.all()
    wb = workbook()
    ws = wb.active
    ws['B1'] = 'Members Report'

    ws.merge.cells('B1:E1')

    ws['B3'] = 'FirstName'
    ws['C3'] = 'LastName'
    ws['D3'] = 'Phone'
    ws['D3'] = 'joined date'

    count = 4

    for member in memberset:
      ws.cell(row = count, column = 2).value = member.firstname
      ws.cell(row = count, column = 3).value = member.lastname
      ws.cell(row = count, column = 4).value = member.phone
      ws.cell(row = count, column = 5).value = member.joined_date
      count += 1

    file_name = 'MembersReport.xlsx'

    response = HttpResponse(content_type='application/ms-excel')
    content = "attachment; filename = {0}".format(file_name)
    response['Content-Disposition'] = content
    wb.save(response)

    return response
but after reload the project and trying it again I get a different issue. no sure if I must opena new post or you can help me out using this same thread.

error:
Quote:Traceback (most recent call last):
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\views\generic\base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\lib\site-packages\django\views\generic\base.py", line 143, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\pm25383\OneDrive - Alliance\Documentos\Python Project\myworld\my_tennis_club\members\views.py", line 46, in get
wb = workbook()
TypeError: 'module' object is not callable
Reply
#5
Workbook() not workbook(). From openpyxl you want to import Workbook, which is a class, not workbook which is a module.
from openpyxl import Workbook
wb = Workbook()
Or if you could do this:
from openpyxl import workbook
wb = workbook.Workbook()
But that would be silly.
Reply
#6
(Oct-30-2024, 06:05 PM)deanhystad Wrote: Workbook() not workbook(). From openpyxl you want to import Workbook, which is a class, not workbook which is a module.
from openpyxl import Workbook
wb = Workbook()
Or if you could do this:
from openpyxl import workbook
wb = workbook.Workbook()
But that would be silly.

Dully noted, I am clear now, thanks a lot for your kind reponse... I really appreciate it.
I am new here and most the concepts are new and I am still learning them, but you helped me to understood better how it works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pylance: "... is not a known attribute of module ..." ? MvGulik 5 3,663 Jul-08-2025, 04:41 PM
Last Post: MvGulik
  How to copy work sheet data one workbook to other? sayyedkamran 2 2,225 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Active Directory integration dady 2 1,779 Oct-13-2023, 04:02 AM
Last Post: deanhystad
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 4,451 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  read active document name - other than from the window title ineuw 0 1,453 Sep-11-2023, 09:06 AM
Last Post: ineuw
  xlwings error when reading a workbook Mishal0488 1 3,349 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  Openpyxl module breaking my code EnderSM 5 4,449 May-26-2023, 07:26 PM
Last Post: snippsat
  Help in opening and editing an excel workbook from a web-browser test 4 3,522 Aug-10-2022, 02:31 PM
Last Post: test
  Module 'time' has no attribute 'clock' Sophie 4 8,149 Jan-25-2022, 08:05 PM
Last Post: Sophie
  Read and write active Excel file euras 4 10,141 Jun-29-2021, 11:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020