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:
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?
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?
