We have multiple scripts which write into XLSX with the same format, into a different format or slide variation. I am trying to write an API which saves time for other programs, but I need input on restructuring the file.
import openpyxl
from xlsxwriter.workbook import Workbook
class xlsx_extend(object):
"""This class contain api of xlsx functionality"""
def __init__(self, name):
"""create xls file"""
self.xlsx_name = Workbook(name)
self.create_format('blue', 12)
def create_format(self, color, size):
"""write header"""
self.xlsx_format = self.xlsx_name.add_format()
self.xlsx_format.set_font_size(size)
self.xlsx_format.set_bold()
self.xlsx_format.set_color(color)
self.xlsx_format.set_border()
self.xlsx_format.set_center_across()
self.xlsx_format.set_align('center')
self.xlsx_format.set_rotation(90)
def add_sheet(self, ws_name):
"""Adding worksheet name"""
return self.xlsx_name.add_worksheet(ws_name)
def rotate_text_format(self, angle):
"""rotate the text data"""
self.xlsx_format.set_rotation(angle)
def get_xlsx_name(self):
"""Returing xlsx name for derive class"""
return self.xlsx_name
def close_xlsx(self):
"""close xlsx File"""
self.xlsx_name.close()
class xlsx_sheet:
"""This is xlsx worksheet class"""
def __init__(self, excel_sheet, ws_name):
"""Initialze Base function and Create Worksheet"""
self.excel_sheet = excel_sheet
self.ws_name_worksheet = excel_sheet.add_sheet(ws_name)
self.ws_name_worksheet.set_column(0, 0, 40)
def write(self, row, col, data, format=None):
"""write Add in xlsx File"""
self.ws_name_worksheet.write(row, col, data)
def write_column(self, row, col, data, format=None):
"""write data in column than need pass data as list"""
self.ws_name_worksheet.write_column(row, col, data)
def write_row(self, row, col, data, format=None):
"""write data in row than need to pass as list"""
self.ws_name_worksheet.write_row(row, col, data, self.excel_sheet.xlsx_format)
def write_comment(self, row, col, data):
"""write comment in cell"""
self.ws_name_worksheet.write_comment(row, col, data, {'width': 400, 'height': 400})
def text_condition_format(self, first_row, first_col, last_row, last_col, value, format):
"""Adding text condition format"""
self.ws_name_worksheet.conditional_format(first_row, first_col, last_row, last_col,
{'type': 'text',
'criteria': 'containing',
'value': value,
'format': format
})
And another module with it's call API like:
report_xlsx = xlsxutils.xlsx_extend("report.xlsx")
red_format = report_xlsx.xlsx_name.add_format({'bg_color': '#FF0000'})
green_format = report_xlsx.xlsx_name.add_format({'bg_color':'#99CC32'})
gray_format = report_xlsx.xlsx_name.add_format({'bg_color':'#53868B'})
blue_format = report_xlsx.xlsx_name.add_format({'bg_color':'#3298FE'})
light_red_format = report_xlsx.xlsx_name.add_format({'bg_color':'#FF9999'})
report_worksheet = xlsxutils.xlsx_sheet(report_xlsx, "Report")
report_worksheet.text_condition_format(1, 1, row, col, 'P', green_format)
report_worksheet.text_condition_format(1, 1, row, col, 'F', red_format)
report_worksheet.text_condition_format(1, 1, row, col, 'AB', red_format)
report_worksheet.text_condition_format(1, 1, row, col, 'RU', light_red_format)
report_worksheet.text_condition_format(1, 1, row, col, 'QU', light_red_format)
report_worksheet.text_condition_format(1, 1, row, col, 'M', gray_format)
report_worksheet.text_condition_format(1, 1, row, col, 'UN', blue_format)
report_xlsx.close_xlsx()
Can you do code review and provide your input?