-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
112 lines (88 loc) · 3.04 KB
/
util.py
File metadata and controls
112 lines (88 loc) · 3.04 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import annotations
import openpyxl
class CellFormat:
thin = openpyxl.styles.Side(border_style="thin", color="000000")
thin_border = openpyxl.styles.Border(top=thin, left=thin, right=thin, bottom=thin)
bold_font = openpyxl.styles.Font(bold=True)
colors = {}
alignment = openpyxl.styles.alignment.Alignment(horizontal="center", vertical="center")
currency_format = '[$$-409]#,##0.00'
gb_format = '0.0 "GB"'
tb_format = '0.0 "TB"'
sheet = openpyxl.worksheet.worksheet.Worksheet(None)
def get_color(self, color: str):
if not color in self.colors:
self.colors[color] = openpyxl.styles.PatternFill("solid", color)
return self.colors[color]
def __init__(self, sheet=None):
self.has_border = False
self.is_centered = False
self.is_bold = False
self.is_currency = False
self.is_gb = False
self.is_tb = False
self.fill_color = None
self.data = None
if not sheet is None:
self.sheet = sheet
def border(self, color:str="000000") -> CellFormat:
self.has_border = True
return self
def color(self, color:str) -> CellFormat:
if not color is None:
self.fill_color = self.get_color(color)
return self
def value(self, value:str) -> CellFormat:
cell = openpyxl.cell.cell.Cell(self.sheet, column=1, row=1, value=value)
return self.apply(cell)
def center(self) -> CellFormat:
self.is_centered = True
return self
def bold(self) -> CellFormat:
self.is_bold = True
return self
def currency(self) -> CellFormat:
self.is_currency = True
self.is_gb = False
self.is_tb = False
return self
def gb(self) -> CellFormat:
self.is_currency = False
self.is_gb = True
self.is_tb = False
return self
def tb(self) -> CellFormat:
self.is_currency = False
self.is_gb = False
self.is_tb = True
return self
def header(self, color:str=None) -> CellFormat:
return self.border().bold().center().color(color)
def apply(self, cell):
if (self.is_bold):
cell.font = self.bold_font
if (self.has_border):
cell.border = self.thin_border
if self.is_centered:
cell.alignment = self.alignment
if not self.fill_color is None:
cell.fill = self.fill_color
if self.is_currency:
cell.number_format = self.currency_format
if self.is_gb:
cell.number_format = self.gb_format
if self.is_tb:
cell.number_format = self.tb_format
# if not self.value is None:
# cell.value = self.data
return cell
def generator(self, row):
for value in row:
yield self.apply(
openpyxl.cell.cell.Cell(
self.sheet,
column="A",
row=1,
value=value
)
)