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?
#1
Hi guys,

I would like to create a subclass of XlsxWriter in order to add new methods to semplify my work. For example, the script that I have in my mind, is written more or less in this way:
# import my custom class (a subclass of xlsxwriter):
import XlsxWriterPlus as xwp

# create a workbook with the first sheet named "SHEET 1":
wb =  xwp.Workbook("ciao.xlsx")
ws_1 = wb.add_worksheet("SHEET 1")

# use my custom method on the sheet object (ws_1): 
a = ws_1.my_method(arg1, arg2, ..)

# now the object wb has his fisrt worksheet compiled as I wanted. I can close the excel file:
a.close()
How can I reach my goal? Keep in mind that I didn't ever create a subclass of another one.. I'm working with the classes in these weeks. I just want to add new methods in xlsxwriter and use them in my scripts.

maybe what I should write to create my new class should be something like this:
# import just what I need:
from xlsxwriter.worksheet import Worksheet

# create my subclass:
class XlsxWriterPlus(Worksheet):
    def my_method(self, arg1, arg2, ..)
        ..
        ..
        ..

    def my_method_2(self, arg1, arg2, ..)
        ..
        ..
        ..

    def my_method_3(self, arg1, arg2, ..)
        ..
        ..
        ..
what do you think? can you please give me a hand to understand better how to proceed?
Reply
#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
#3
I solved! thank goodness I found an article about my question in the official documentation:

https://xlsxwriter.readthedocs.io/exampl...ance1.html
Reply


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,678 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  xlsxwriter + mplfinance: Plot Stock Chart in Excel Worksheet KMV 1 2,005 Mar-09-2021, 09:44 PM
Last Post: KMV
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,441 Mar-09-2021, 12:24 PM
Last Post: KMV
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,838 Jan-22-2021, 05:43 AM
Last Post: buran
  xlsxwriter in windows dfstrottersfan 2 2,285 Sep-23-2020, 11:41 AM
Last Post: dfstrottersfan
  use of subclass ebolisa 4 2,164 Sep-17-2020, 01:08 PM
Last Post: jefsummers
  Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter jackie 1 3,138 May-27-2020, 11:47 PM
Last Post: mcmxl22
  Accessing subclass with a variable David_S 2 2,100 May-19-2020, 05:55 PM
Last Post: David_S
  XlsxWriter and Python inheritance aquerci 1 2,463 May-05-2020, 09:36 AM
Last Post: buran
  Design Pattern for accessing subclass attributes UGuntupalli 2 2,047 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