1 | #!/usr/bin/env python |
---|
2 | # -*- coding: iso-8859-1 -*- |
---|
3 | |
---|
4 | """ |
---|
5 | |
---|
6 | ========== |
---|
7 | aeresrh.py |
---|
8 | ========== |
---|
9 | |
---|
10 | DESCRIPTION |
---|
11 | =========== |
---|
12 | |
---|
13 | extract information from aeresrh.xls |
---|
14 | |
---|
15 | .. option:: --ifile <file> |
---|
16 | |
---|
17 | file to read |
---|
18 | |
---|
19 | .. only:: man |
---|
20 | |
---|
21 | Figure is visible on PDF and HTML documents only. |
---|
22 | |
---|
23 | .. only:: html or latex |
---|
24 | |
---|
25 | .. graphviz:: |
---|
26 | |
---|
27 | digraph aeresrh { |
---|
28 | rh [shape=diamond, |
---|
29 | fontname=Courier, |
---|
30 | label='{ifile}' |
---|
31 | |
---|
32 | aeresrh [shape=box, |
---|
33 | fontname=Courier, |
---|
34 | color=blue, |
---|
35 | URL="http://forge.ipsl.jussieu.fr/pulsation/browser//branches/aeresrh/scripts/aeresrh.py", |
---|
36 | label="${PROJECT}/aeres/scripts/aeresrh.py"]; |
---|
37 | } |
---|
38 | |
---|
39 | SEE ALSO |
---|
40 | ======== |
---|
41 | |
---|
42 | https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html |
---|
43 | |
---|
44 | EXAMPLES |
---|
45 | ======== |
---|
46 | |
---|
47 | aeresrh.py -ifile ${PROJECT}/data/aeresrh.xls |
---|
48 | |
---|
49 | TODO |
---|
50 | ==== |
---|
51 | |
---|
52 | test with real file (encoding, francais, etc) |
---|
53 | |
---|
54 | missing values |
---|
55 | |
---|
56 | remove hard coded number of columns for surname, firstname, ... |
---|
57 | |
---|
58 | coding rules (pylint) |
---|
59 | |
---|
60 | EVOLUTIONS |
---|
61 | ========== |
---|
62 | |
---|
63 | $Id$ |
---|
64 | |
---|
65 | $URL$ |
---|
66 | |
---|
67 | - fplod 20120405 |
---|
68 | |
---|
69 | * gestion des temps (arrivée, départ) (to be cont.) |
---|
70 | * creation draft |
---|
71 | thanks to http://www.portailsig.org/content/python-lire-et-ecrire-des-fichiers-microsoft-excel-application-quantum-gis |
---|
72 | and http://scienceoss.com/read-excel-files-from-python/ |
---|
73 | """ |
---|
74 | |
---|
75 | import sys |
---|
76 | import os |
---|
77 | from os.path import dirname, basename |
---|
78 | import glob |
---|
79 | from optparse import OptionParser |
---|
80 | |
---|
81 | import datetime |
---|
82 | |
---|
83 | try: |
---|
84 | import xlrd |
---|
85 | except ImportError: |
---|
86 | print("Failed to import xlrd from any known place") |
---|
87 | sys.exit(1) |
---|
88 | |
---|
89 | def get_option_parser (): |
---|
90 | """parse CLI arguments |
---|
91 | |
---|
92 | :returns: parser |
---|
93 | :rtype: :class:`optparse.OptionParser` |
---|
94 | """ |
---|
95 | |
---|
96 | parser = OptionParser('%prog [--verbose ] [--ifile file') |
---|
97 | parser.add_option ('-V', '--verbose', |
---|
98 | help='produce verbose output', |
---|
99 | action='store_true', |
---|
100 | default=False, |
---|
101 | dest='is_verbose') |
---|
102 | parser.add_option ('-i', '--ifile', |
---|
103 | help='file to be read', |
---|
104 | type='string', |
---|
105 | dest='ifile', |
---|
106 | default=None, |
---|
107 | action='store') |
---|
108 | |
---|
109 | return parser |
---|
110 | |
---|
111 | def readrh(ifile, is_verbose): |
---|
112 | |
---|
113 | """ |
---|
114 | read ifile |
---|
115 | |
---|
116 | return ++ |
---|
117 | """ |
---|
118 | |
---|
119 | if is_verbose == True: |
---|
120 | print('iii : read %s' % ifile) |
---|
121 | |
---|
122 | # ouverture du fichier Excel |
---|
123 | wb = xlrd.open_workbook(ifile) |
---|
124 | |
---|
125 | # feuilles dans le classeur |
---|
126 | if is_verbose == True: |
---|
127 | print('sheets name %s' % ( wb.sheet_names())) |
---|
128 | |
---|
129 | # lecture des données dans la première feuille |
---|
130 | sh = wb.sheet_by_index(0) |
---|
131 | |
---|
132 | # |
---|
133 | if is_verbose == True: |
---|
134 | for rownum in range(sh.nrows): |
---|
135 | print('values of in row %d : %s' % (rownum, sh.row_values(rownum))) |
---|
136 | |
---|
137 | if is_verbose == True: |
---|
138 | for colnum in range(sh.ncols): |
---|
139 | print('values of in col %d : %s' % (colnum, sh.col_values(colnum))) |
---|
140 | |
---|
141 | colsurname = 1 |
---|
142 | colfirstname = 2 |
---|
143 | colarrival = 13 |
---|
144 | coldeparture = 14 |
---|
145 | colpubli = 16 |
---|
146 | |
---|
147 | datemode = 0 |
---|
148 | |
---|
149 | surname = sh.col_values(colsurname) |
---|
150 | # remove the first element because it is the title |
---|
151 | surname[0] = '' |
---|
152 | surname = filter(lambda surname: surname != '', surname) |
---|
153 | |
---|
154 | if is_verbose == True: |
---|
155 | print(' nb surname %d' % len(surname)) |
---|
156 | print(' surname %s' % surname) |
---|
157 | |
---|
158 | firstname = sh.col_values(colfirstname) |
---|
159 | # remove the first element because it is the title |
---|
160 | firstname[0] = '' |
---|
161 | firstname = filter(lambda firstname: firstname != '', firstname) |
---|
162 | |
---|
163 | if is_verbose == True: |
---|
164 | print(' nb firstname %d' % len(firstname)) |
---|
165 | print(' firstname %s' % firstname) |
---|
166 | |
---|
167 | temp = sh.col_values(colarrival) |
---|
168 | # remove the first element because it is the title |
---|
169 | temp[0] = '' |
---|
170 | temp = filter(lambda temp: temp != '', temp) |
---|
171 | # transform to datetime |
---|
172 | arrival = [] |
---|
173 | for rownum in range(len(temp)): |
---|
174 | #++ pb sh.datemodearrival = datetime.datetime(*xlrd.xldate_as_tuple(temp[rownum],sh.datemode)) |
---|
175 | arrival.append(datetime.datetime(*xlrd.xldate_as_tuple(temp[rownum],datemode))) |
---|
176 | |
---|
177 | if is_verbose == True: |
---|
178 | print(' nb arrival %d' % len(arrival)) |
---|
179 | print(' arrival %s' % arrival) |
---|
180 | |
---|
181 | temp = sh.col_values(coldeparture) |
---|
182 | # remove the first element because it is the title |
---|
183 | temp[0] = '' |
---|
184 | temp = filter(lambda temp: temp != '', temp) |
---|
185 | # transform to datetime |
---|
186 | departure = [] |
---|
187 | for rownum in range(len(temp)): |
---|
188 | #++ pb sh.datemodedeparture = datetime.datetime(*xlrd.xldate_as_tuple(temp[rownum],sh.datemode)) |
---|
189 | departure.append(datetime.datetime(*xlrd.xldate_as_tuple(temp[rownum],datemode))) |
---|
190 | |
---|
191 | if is_verbose == True: |
---|
192 | print(' nb departure %d' % len(departure)) |
---|
193 | print(' departure %s' % departure) |
---|
194 | |
---|
195 | publi = sh.col_values(colpubli) |
---|
196 | # remove the first element because it is the title |
---|
197 | publi[0] = '' |
---|
198 | publi = filter(lambda publi: publi != '', publi) |
---|
199 | |
---|
200 | if is_verbose == True: |
---|
201 | print(' nb publi %d' % len(publi)) |
---|
202 | print(' publi %s' % publi) |
---|
203 | |
---|
204 | return surname, firstname, arrival, departure, publi |
---|
205 | |
---|
206 | def aeresrh(): |
---|
207 | """main |
---|
208 | """ |
---|
209 | try: |
---|
210 | parser = get_option_parser () |
---|
211 | (fromcli, args) = parser.parse_args() |
---|
212 | except IOError, msg: |
---|
213 | parser.error(str(msg)) |
---|
214 | |
---|
215 | is_verbose = fromcli.is_verbose |
---|
216 | ifile = fromcli.ifile |
---|
217 | if fromcli.is_verbose == True: |
---|
218 | print ('ifile : %s' % (ifile)) |
---|
219 | |
---|
220 | surname, firstname, arrival, departure,publi = readrh(ifile, is_verbose) |
---|
221 | print (' surname %s' % (surname)) |
---|
222 | |
---|
223 | # Run main, if called from the command line |
---|
224 | if __name__ == '__main__': |
---|
225 | aeresrh() |
---|
226 | |
---|