Masse salariale, avec un peu de formattage pour le heading
[auf_rh_dae.git] / project / rh / ods.py
1 # encoding: utf-8
2
3 from decimal import Decimal
4
5 import odf.opendocument
6 import odf.style
7 import odf.table
8
9
10 class Wrapper(object):
11
12 def __init__(self, *args, **kwargs):
13 self.__wrapped = self._wrapper_constructor(*args, **kwargs)
14
15 def __getattr__(self, attr):
16 return getattr(self.__wrapped, attr)
17
18
19 class OpenDocumentSpreadsheet(Wrapper):
20 _wrapper_constructor = staticmethod(
21 odf.opendocument.OpenDocumentSpreadsheet
22 )
23
24 def __init__(self, *args, **kwargs):
25 super(OpenDocumentSpreadsheet, self).__init__(*args, **kwargs)
26 self._automatic_style_idx = 0
27
28 def add_table(self, **kwargs):
29 table = Table(**kwargs)
30 table._doc = self
31 self.spreadsheet.addElement(table)
32 return table
33
34 def add_automatic_style(self, **kwargs):
35 name = 'auto_style_%d' % self._automatic_style_idx
36 style = odf.style.Style(name=name, **kwargs)
37 self.automaticstyles.addElement(style)
38 self._automatic_style_idx += 1
39 return style
40
41
42 class Table(Wrapper):
43 _wrapper_constructor = staticmethod(odf.table.Table)
44
45 def add_row(self, values=[], **kwargs):
46 # attributs appartenant à table-column-poperties
47 # props = {}
48 # for attr in ['rowheight']:
49 # if attr in kwargs:
50 # props[attr] = kwargs.pop(attr)
51
52 style = self._doc.add_automatic_style(family='table-row')
53 if 'rowheight' in kwargs:
54 style.addElement(odf.style.TableRowProperties(
55 rowheight=kwargs['rowheight']))
56 kwargs['stylename'] = style.getAttribute('name')
57 del kwargs['rowheight']
58
59 row = TableRow(**kwargs)
60 row._doc = self._doc
61 for value in values:
62 row.add_cell(value, verticalalign='middle')
63 self.addElement(row)
64 return row
65
66 def add_column(self, **kwargs):
67
68 # attributs appartenant à table-column-poperties
69 props = {}
70 for attr in ['columnwidth']:
71 if attr in kwargs:
72 props[attr] = kwargs.pop(attr)
73
74 if props:
75 style = self._doc.add_automatic_style(family='table-column')
76 style.addElement(odf.style.TableColumnProperties(**props))
77 kwargs['stylename'] = style.getAttribute('name')
78 col = odf.table.TableColumn(**kwargs)
79 self.addElement(col)
80 return col
81
82
83 class TableRow(Wrapper):
84 _wrapper_constructor = staticmethod(odf.table.TableRow)
85
86 def add_cell(self, value=None, **kwargs):
87 if value:
88 if isinstance(value, basestring):
89 kwargs['stringvalue'] = value
90 elif isinstance(value, (int, float, Decimal)):
91 kwargs['valuetype'] = 'float'
92 kwargs['value'] = float(value)
93
94 if 'verticalalign' in kwargs:
95 style = self._doc.add_automatic_style(family='table-cell')
96 style.addElement(odf.style.TableCellProperties(
97 verticalalign=kwargs['verticalalign']))
98 kwargs['stylename'] = style.getAttribute('name')
99 del kwargs['verticalalign']
100
101 cell = odf.table.TableCell(**kwargs)
102 self.addElement(cell)
103 return cell