S
I went on the way to create the classrooms, and I created a clear label right inside. __init__♪I hope it's not too stunning. I finally made this decision: repetitive functions are described inside the classroom, so they don't need to be rewritten in performance. And the creation of a type of OS class is clearly out. So there's no "closed" consequences.from pathlib import Path
import os
import sys
from copy import copy # , deepcopy
from openpyxl.styles import Color, PatternFill, Font, Border
import openpyxl as opx
from abc import ABC, abstractmethod # , abstractproperty
import win32com.client
class IZI_report(ABC):
"""Class for reports."""
def __init__(self, path_to_files_folder: Path):
print('IZI_report(ABC) init')
self.current_os_is_win = sys.platform in ['win32', 'cygwin'] and os.name == "nt"
if self.current_os_is_win:
print('win32com.client')
import win32com.client
if self.current_os_is_win:
self.xlsx_formater = self.Windows_excel_file_formatting()
else:
self.xlsx_formater = self.Common_excel_file_formatting()
self.path_to_files_folder = path_to_files_folder
self.response = { # dict_to_return
"exit_is_ok": False,
"exit_message": "",
"files": dict(),
'zip_path': None,
}
@ abstractmethod
def files_reading(self, path_to_files_folder):
...
@ abstractmethod
def data_processing(self, **pd_files):
...
@ abstractmethod
def excel_writer(self, path_to_files_folder, client_name, files):
...
class Windows_excel_file_formatting(ABC):
def search_and_colorise(self, work_sheet, searched_texts_list, color=4):
""""""
if type(searched_texts_list) is str:
raise Exception('list of str expected!')
for seached in searched_texts_list:
work_sheet.Cells.Find(seached).Interior.ColorIndex = color
def header_wrap_and_size(self, ws, size):
""""""
ws.Rows('1:1').WrapText = True
ws.Columns(ws.Range("A1").CurrentRegion.Columns).ColumnWidth = size
ws.Rows("1:1").EntireRow.AutoFit()
@ abstractmethod
def excel_file_formatting(self, file_to_format):
...
class Common_excel_file_formatting(ABC):
"""Format xlsx file."""
def search_and_colorise(self, work_sheet, searched_texts_list, color='EE1111'):
"""Renewed."""
if type(searched_texts_list) is str:
raise Exception('list of str expected!')
# openpx style
fill_color = PatternFill(start_color=color,
end_color=color,
fill_type='solid')
for searched in searched_texts_list:
for j in range(1, work_sheet.max_column + 1):
print(work_sheet.cell(row=1, column=j).value)
if work_sheet.cell(row=1, column=j).value:
if work_sheet.cell(row=1, column=j).value.find(searched) >= 0:
work_sheet.cell(row=1, column=j).fill = fill_color
def header_wrap_and_size(self, ws, size):
"""Renewed."""
for j in range(1, ws.max_column + 1):
ws.cell(row=1, column=j).alignment = \
opx.styles.Alignment(
horizontal='center', # 'general',
vertical='bottom',
text_rotation=0,
wrap_text=True,
shrink_to_fit=False,
indent=0)
ws.column_dimensions[opx.utils.cell.get_column_letter(j)].width = size
@ abstractmethod
def excel_file_formatting(self, file_to_format):
...
At the same time, it's a few, I'd say, "indecent" In order to create a class, you must inherit from the parent with a clear indication of the name of the abstract class. There you go.class Windows_excel_file_formatting(IZI_report.Windows_excel_file_formatting)It's working, but I'd like something better.Example of a test class that reads a disk file, followed by the marking of a function described in search_and_colorise ...class Test(IZI_report):
def __init__(self, path: Path):
super().__init__(path)
self.SNAPSHOTS_PATTERNS = ["rec", "adj", "rei"]
self.separators = " _-"
def files_reading(self, path_to_files_folder):
...
def data_processing(self, **pd_files):
...
def excel_writer(self, path_to_files_folder, client_name, files):
...
class Windows_excel_file_formatting(IZI_report.Windows_excel_file_formatting):
def excel_file_formatting(self, file_to_format):
print('excel_file_formatting')
Excel = win32com.client.DispatchEx("Excel.Application")
print(1)
wb = Excel.Workbooks.Open(os.path.join(os.getcwd(), "sh1.xlsx"))
print(2)
ws_rec = wb.Worksheets("Аркуш1")
print("---- before =====")
self.search_and_colorise(ws_rec, ("01_", '10_', '20_'))
wb.Save()
print(3)
wb.Close()
Excel.Application.Quit()
Excel.Quit()
class Common_excel_file_formatting():
"""Format xlsx file."""
def excel_file_formatting(self, file_to_format):
wb = opx.load_workbook("sh1.xlsx")
ws_rec = wb["Sheet01"]
path_to = Path("nothng.txt")
zz = Test(path_to)
zz.xlsx_formater.excel_file_formatting('ff')
Everything works, but it's not clear whether it's overexpenditure at the time of the class.I'll be grateful for the clues!