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