55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from abc import ABCMeta
|
|
from openpyxl import Workbook, load_workbook
|
|
|
|
|
|
class ExcelDB:
|
|
|
|
def __init__(self, path, password=None, offset_row=0, offset_column=0):
|
|
self._path = path
|
|
self._pass = password
|
|
self._offset_row = offset_row
|
|
self._offset_column = offset_column
|
|
|
|
def init(self):
|
|
pass
|
|
|
|
def save(self, model):
|
|
wb = load_workbook(self._path)
|
|
ws = wb[model.Meta.table]
|
|
ws["A1"] = model.username
|
|
wb.save(self._path)
|
|
|
|
def create(self, model):
|
|
wb = load_workbook(self._path)
|
|
ws = wb[model.Meta.table]
|
|
next_row = len(tuple(ws.rows)) + 1
|
|
next_column = 1 + self._offset_column
|
|
for e in model.__dict__:
|
|
if e in ['__module__', 'Meta', '__doc__']:
|
|
continue
|
|
c = ws.cell(next_row, next_column)
|
|
c.value = model.__dict__.get(e)
|
|
next_column = next_column + 1
|
|
wb.save(self._path)
|
|
|
|
def read(self, model):
|
|
wb = load_workbook(self._path)
|
|
ws = wb[model.Meta.table]
|
|
|
|
|
|
def create_tables(self, tables):
|
|
wb = Workbook()
|
|
wb.remove_sheet(wb.active)
|
|
for t in tables:
|
|
wb.create_sheet(t.Meta.table)
|
|
ws = wb[t.Meta.table]
|
|
i = 1
|
|
for e in t.__dict__:
|
|
if e in ['__init__', '__module__', 'Meta', '__doc__']:
|
|
continue
|
|
c = ws.cell(1, i)
|
|
c.value = t.__dict__.get(e).caption
|
|
i = i + 1
|
|
wb.save(self._path)
|
|
|