Python Forum
How can I create a subclass of XlsxWriter? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can I create a subclass of XlsxWriter? (/thread-26446.html)



How can I create a subclass of XlsxWriter? - aquerci - May-02-2020

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?


RE: How can I create a subclass of XlsxWriter? - aquerci - May-02-2020

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?


RE: How can I create a subclass of XlsxWriter? - aquerci - May-04-2020

I solved! thank goodness I found an article about my question in the official documentation:

https://xlsxwriter.readthedocs.io/example_inheritance1.html