-
Notifications
You must be signed in to change notification settings - Fork 0
/
kexcel.py
133 lines (111 loc) · 3.4 KB
/
kexcel.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
u"""Generating Excel files."""
import collections
import xlsxwriter
import kstyle
import kutil
import unittest
class Excel(object):
def __init__(self, filename):
try:
self.wb = xlsxwriter.Workbook(filename)
except:
kutil.exception_exit(u'error creating output file: {}'.format(filename))
self.fmt = dict(( k, self.wb.add_format(v)) for k, v in kstyle.excel_format.items())
self.sheets = []
def sheet(self, name, cols):
return Sheet(self, name, cols)
def close(self):
for s in self.sheets:
s.close()
self.wb.close()
class Sheet(object):
def __init__(self, xl, name, cols):
self.isopen = True
self.xl = xl
self.ws = self.xl.wb.add_worksheet(name)
self.cols = cols
self.widths = collections.defaultdict(int)
self.r = 1
self.c = 0
self.xl.sheets.append(self)
def close(self):
if not self.isopen:
return
self.ws.set_row(0, None, self.xl.fmt[u"bold"])
self.r = 0
self.c = 0
for col in self.cols:
name = col[0]
self.write_string(name)
for c, col in enumerate(self.cols):
fmt = col[1] if len(col) > 1 else None
width = col[2] if len(col) > 2 else None
if width is None:
width = self._autoscale(c)
if fmt is None:
self.ws.set_column(c, c, width)
else:
self.ws.set_column(c, c, width, self.xl.fmt[fmt])
self.ws.freeze_panes(1, 0)
self.isopen = False
def next_row(self):
self.r += 1
self.c = 0
def write_number(self, v):
if v is not None:
self.ws.write_number(self.r, self.c, v)
self._tell(len(unicode(v)))
self.c += 1
def write_string(self, v):
if v is not None:
self.ws.write_string(self.r, self.c, v)
self._tell(len(v))
self.c += 1
def write_url(self, url, v):
if v is not None:
self.ws.write_url(self.r, self.c, url, None, v)
self._tell(len(v))
self.c += 1
def write_rich(self, v):
l = []
s = u""
for fmt, txt in v:
if fmt != u"normal":
l.append(self.xl.fmt[fmt])
l.append(txt)
s += txt
if len(l) > 0:
self.ws.write_rich_string(self.r, self.c, *l)
self._tell(len(s))
self.c += 1
def _autoscale(self, c):
t = 10
k = 0.8
w = self.widths[c] + 1
if w > t:
w = t + int(k * (w - t))
return w
def _tell(self, w):
self.widths[self.c] = max(self.widths[self.c], w)
def rich_simplify(l):
if len(l) == 0:
return l
r = []
x0, y0 = l[0]
for x1, y1 in l[1:]:
if x0 == x1:
y0 += y1
else:
r.append((x0, y0))
x0, y0 = x1, y1
r.append((x0, y0))
return r
#### Unit tests
class TestKExcel(unittest.TestCase):
def test_rich_simplify(self):
self.assertEqual(rich_simplify([]), [])
self.assertEqual(rich_simplify([("a", "x")]), [("a", "x")])
self.assertEqual(rich_simplify([("a", "x"), ("a", "y")]), [("a", "xy")])
self.assertEqual(rich_simplify([("a", "x"), ("b", "y")]), [("a", "x"), ("b", "y")])
if __name__ == u'__main__':
unittest.main()