Python Forum
How can I create a subclass of XlsxWriter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I create a subclass of XlsxWriter?
#2
I just tried to write my custom method "write_table" in a custom subclass of "Worksheet" (WorksheetPlus). Below my code:
from xlsxwriter.worksheet import Worksheet

class WorksheetPlus(Worksheet):
    # custom method:     
    def write_table(self, info, row, column):
        for dic in info:
            y = column
            for key in dic:
                self.write(row, y, dic[key])
                y += 1
            row += 1
        
# test:
if __name__ == "__main__":
    from xlsxwriter.workbook import Workbook
    info = [{1:1, 2:2, 3:3, 4:4, 5:5}, {1:11, 2:22, 3:33, 4:444, 5:55}]
    obj_wb = Workbook("ciao.xlsx")
    obj_ws = obj_wb.add_worksheet("FIRST")
    obj_ws.write_table(info, 0, 0) #  why this statement doesn't work?
Unfortunately if I test this code, it doesn't work:
Quote:λ python WorksheetPlus.py

Traceback (most recent call last):
File "WorksheetPlus.py", line 22, in <module>
obj_ws.write_table(info, 0, 0) # come mai non funziona?
AttributeError: 'Worksheet' object has no attribute 'write_table' <------------- !!!

Why is not my custom method an attribute of "Worksheet" object? can you help me to fix my code?
Reply


Messages In This Thread
RE: How can I create a subclass of XlsxWriter? - by aquerci - May-02-2020, 07:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,843 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  xlsxwriter + mplfinance: Plot Stock Chart in Excel Worksheet KMV 1 2,062 Mar-09-2021, 09:44 PM
Last Post: KMV
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,525 Mar-09-2021, 12:24 PM
Last Post: KMV
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,916 Jan-22-2021, 05:43 AM
Last Post: buran
  xlsxwriter in windows dfstrottersfan 2 2,360 Sep-23-2020, 11:41 AM
Last Post: dfstrottersfan
  use of subclass ebolisa 4 2,245 Sep-17-2020, 01:08 PM
Last Post: jefsummers
  Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter jackie 1 3,225 May-27-2020, 11:47 PM
Last Post: mcmxl22
  Accessing subclass with a variable David_S 2 2,158 May-19-2020, 05:55 PM
Last Post: David_S
  XlsxWriter and Python inheritance aquerci 1 2,521 May-05-2020, 09:36 AM
Last Post: buran
  Design Pattern for accessing subclass attributes UGuntupalli 2 2,114 Jul-30-2019, 11:09 PM
Last Post: UGuntupalli

Forum Jump:

User Panel Messages

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