-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_result.py
More file actions
67 lines (43 loc) · 1.93 KB
/
Copy pathsave_result.py
File metadata and controls
67 lines (43 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from abc import ABC, abstractmethod
import xlsxwriter
import pandas as pd
from dto import ProductCardReportDTO
class ResultWriter(ABC):
@abstractmethod
def write():
pass
class XlsxResultWriter(ResultWriter):
def __init__(self, result_file: str,) -> None:
self.file_path = result_file
self.result_file = xlsxwriter.Workbook(result_file)
self.current_row = 0
def close(self) -> None:
self.result_file.close()
def write(self, product: ProductCardReportDTO, worksheet: str='Кофе') -> None:
if self.current_row == 0:
# Если это первая запись, создаём новый лист и добавляем заголовки
self.page = self.result_file.add_worksheet(worksheet)
headers = ['Id', 'Название', 'Ссылка на продукт', 'Цена', 'Акционная цена', 'Бренд']
for col, header in enumerate(headers):
self.page.write(self.current_row, col, header)
self.current_row += 1
print(product)
self.page.write(self.current_row, 0, product.article_number)
self.page.write(self.current_row, 1, product.name)
self.page.write(self.current_row, 2, product.product_link)
self.page.write(self.current_row, 3, product.regular_price)
self.page.write(self.current_row, 4, product.promo_price)
self.page.write(self.current_row, 5, product.brand)
self.current_row += 1
class CleanFiles(ABC):
@staticmethod
@abstractmethod
def clean(file_path: str) -> None:
pass
class CleanXlsxFiles(CleanFiles):
@staticmethod
def clean(file_path: str) -> None:
df = pd.read_excel(file_path)
df = df.dropna(subset=['Id'])
df = df.drop_duplicates(subset=['Id'])
df.to_excel(file_path, index=False)