forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFitSheetWrapper.py
94 lines (82 loc) · 2.69 KB
/
FitSheetWrapper.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
import arial10
import datetime
import Utils
import math
class FitSheetWrapper:
"""Try to fit columns to max size of any entry.
To use, wrap this around a worksheet returned from the
workbook's add_sheet method, like follows:
sheet = FitSheetWrapper(book.add_sheet(sheet_name))
The worksheet interface remains the same: this is a drop-in wrapper
for auto-sizing columns.
"""
def __init__(self, sheet):
self.sheet = sheet
self.widths = {}
self.heights = {}
def write(self, r, c, label='', *args, **kwargs):
if 'bold' in kwargs:
isBold = True
del kwargs['bold']
else:
isBold = False
self.sheet.write(r, c, label, *args, **kwargs)
if isinstance(label, datetime.date):
label = label.strftime('%b %d, %Y')
elif isinstance(label, datetime.time):
label = '00:00:00'
elif isinstance(label, float): # Assume a float time.
label = '00:00:00'
else:
label = Utils.removeDiacritic( '{}'.format(label) )
if label.find( '\n' ) >= 0:
width, height = arial10.fitWidthHeight(label, isBold)
if height > self.heights.get(r, 0):
self.heights[r] = height
self.sheet.row(r).height = height
else:
width = arial10.fitWidth(label, isBold)
width = min( int(math.ceil(width)), 65535 )
if width > self.widths.get(c, 0):
self.widths[c] = width
self.sheet.col(c).width = width
def __getattr__(self, attr):
return getattr(self.sheet, attr)
StandardCharWidth = arial10.charwidths['0'] * 0.95
class FitSheetWrapperXLSX:
"""Try to fit columns to max size of any entry.
To use, wrap this around a worksheet returned from the
workbook's add_sheet method, like follows:
sheet = FitSheetWrapper(book.add_sheet(sheet_name))
The worksheet interface remains the same: this is a drop-in wrapper
for auto-sizing columns.
"""
def __init__(self, sheet):
self.sheet = sheet
self.widths = {}
self.heights = {}
def write(self, r, c, data, *args, **kwargs):
isBold = kwargs.pop('bold', False)
self.sheet.write(r, c, data, *args, **kwargs)
if isinstance(data, datetime.date):
data = data.strftime('%b %d, %Y')
elif isinstance(data, datetime.time):
data = '00:00:00'
elif isinstance(data, float): # Assume the float is a time.
data = '00:00:00'
else:
data = Utils.removeDiacritic( '{}'.format(data) )
if '\n' in data:
width, height = arial10.fitWidthHeight(data, isBold)
height /= StandardCharWidth
if height > self.heights.get(r, 0.0):
self.heights[r] = height
self.sheet.set_row(r, height)
else:
width = arial10.fitWidth(data, isBold)
width /= StandardCharWidth
if width > self.widths.get(c, 0.0):
self.widths[c] = width
self.sheet.set_column( c, c, width )
def __getattr__(self, attr):
return getattr(self.sheet, attr)