Python Forum
XlsxWriter and Python inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XlsxWriter and Python inheritance
#1
Hi guys,

I have some issues with my code that I can't solve, and an improvment that I can't to implement. my head is splitting.. I'm really stuck with them. I really hope that someone can help me. let's start with the issues.

today I created two subclasses of "Workbook" and "Worksheet" classes taken from XlsxWriter library. below my code:
from xlsxwriter.workbook import Workbook
from xlsxwriter.worksheet import Worksheet
from xlsxwriter.worksheet import convert_cell_args
from xlsxwriter.compatibility import str_types


class WorksheetPlus(Worksheet):
    @convert_cell_args
    def write(self, row, col, *args):
        data = args[0]
        
        # Reverse strings to demonstrate the overridden method.
        if isinstance(data, str_types):
            data = data[::-1]
            return self.write_string(row, col, data)
        else:
            # Call the parent version of write() as usual for other data.
            return super(WorksheetPlus, self).write(row, col, *args)
    
    # custom function
    def write_table(self, info, row, column, header, normal):
        y = column
        for key in info[0]:
            self.write(row, y, info[0][key], header)
            y += 1
        row += 1
        for dic in info[1:]:
            y = column
            for key in dic:
                self.write(row, y, dic[key], normal)
                y += 1
            row += 1

class WorkbookPlus(Workbook):
    # Overwrite add_worksheet() to create a WorksheetPlus object.
    def add_worksheet(self, name=None):
        worksheet = super(WorkbookPlus, self).add_worksheet(name, WorksheetPlus)
        return worksheet

                  
# test:
if __name__ == "__main__":
    info = [{1:"ENGLISH", 2:"ITALIAN", 3:"SPANISH", 4:"RUSSIAN", 5:"JAPANESE", 6:12345, 7:"12345"}, {1:"thanks", 2:"grazie", 3:"gracias", 4:"спасибо", 5:"ありがとう", 6:12345, 7:"12345"}]
    obj_wb = WorkbookPlus("languages.xlsx")
    obj_ws = obj_wb.add_worksheet("EXAMPLE")
    
    header = obj_wb.add_format({'bold': True, 'valign': 'left', 'valign': 'top'})
    normal = obj_wb.add_format({'valign': 'left', 'valign': 'top', 'text_wrap': True})
    obj_ws.write_table(info, 0, 0, header, normal)
    
    obj_wb.close()
my goal is using XlsxWriter module with my custom functions. for example, the "write_table" function, written under the "WorksheetPlus" subclass, help me to create a table in a very simple way (you just have to give him a list of dictionaries).

now, if I try my test code (see at the bottom of my code) it seems works just with the numbers, the strings instead are "mirrored" and with them the cell formats "header" and "normal" are not applied (see the screenshot in attached)

how can I solve these issues with my subclasses?

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
XlsxWriter and Python inheritance - by aquerci - May-04-2020, 10:40 PM
RE: XlsxWriter and Python inheritance - by buran - May-05-2020, 09:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,910 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  xlsxwriter + mplfinance: Plot Stock Chart in Excel Worksheet KMV 1 2,049 Mar-09-2021, 09:44 PM
Last Post: KMV
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,514 Mar-09-2021, 12:24 PM
Last Post: KMV
  xlsxwriter in windows dfstrottersfan 2 2,354 Sep-23-2020, 11:41 AM
Last Post: dfstrottersfan
  Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter jackie 1 3,215 May-27-2020, 11:47 PM
Last Post: mcmxl22
  How can I create a subclass of XlsxWriter? aquerci 2 2,084 May-04-2020, 07:41 PM
Last Post: aquerci
  How inheritance works in Python ARV 1 1,850 Oct-03-2019, 03:06 PM
Last Post: Larz60+
  XlsxWriter: How can I append new data into not blank cells? aquerci 1 9,240 Jun-01-2019, 04:37 AM
Last Post: heiner55
  Downloading a module Xlsxwriter dan789 6 11,284 Jan-26-2019, 02:13 PM
Last Post: dan789
  I am having a big issue in XlsxWriter pratheep 3 2,939 Jan-19-2018, 05:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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