Commit da86ba254084f76c15d0677643b734532ae34d53
1 parent
0c0035fd
Exists in
dev
Support xlsx files in initdb-aprendizations
Showing
2 changed files
with
38 additions
and
6 deletions
Show diff stats
aprendizations/initdb.py
... | ... | @@ -5,8 +5,9 @@ Initializes or updates database |
5 | 5 | ''' |
6 | 6 | |
7 | 7 | # python standard libraries |
8 | -import csv | |
9 | 8 | import argparse |
9 | +import csv | |
10 | +from pandas import read_excel | |
10 | 11 | import re |
11 | 12 | from string import capwords |
12 | 13 | |
... | ... | @@ -32,11 +33,11 @@ def parse_commandline_arguments(): |
32 | 33 | 'command line. If the database does not exist, a new one' |
33 | 34 | ' is created.') |
34 | 35 | |
35 | - argparser.add_argument('csvfile', | |
36 | + argparser.add_argument('files', | |
36 | 37 | nargs='*', |
37 | 38 | type=str, |
38 | 39 | default='', |
39 | - help='CSV file to import (SIIUE)') | |
40 | + help='CSV or Excel files to import (SIIUE)') | |
40 | 41 | |
41 | 42 | argparser.add_argument('--db', |
42 | 43 | default='students.db', |
... | ... | @@ -72,6 +73,30 @@ def parse_commandline_arguments(): |
72 | 73 | |
73 | 74 | |
74 | 75 | # =========================================================================== |
76 | +def get_students_from_xlsx(filename): | |
77 | + excel_settings = { | |
78 | + 'skiprows': 3, | |
79 | + 'converters': { "Número" : str } | |
80 | + } | |
81 | + students = [] | |
82 | + try: | |
83 | + file = read_excel(filename, **excel_settings) | |
84 | + names = file['Aluno'] | |
85 | + nums = file['Número'] | |
86 | + students = [{ | |
87 | + 'uid': num, | |
88 | + 'name': capwords(re.sub(r'\(.*\)', '', name).strip()) | |
89 | + } for num, name in zip(nums, names)] | |
90 | + except FileNotFoundError as e: | |
91 | + print(f'!!! File {filename} not found !!!') | |
92 | + except ValueError as e: | |
93 | + print(f'!!! File {filename} has wrong format !!!') | |
94 | + except Exception as e: | |
95 | + raise e | |
96 | + | |
97 | + return students | |
98 | + | |
99 | +# =========================================================================== | |
75 | 100 | def get_students_from_csv(filename): |
76 | 101 | '''Reads CSV file with enrolled students in SIIUE format. |
77 | 102 | SIIUE names can have suffixes like "(TE)" and are sometimes capitalized. |
... | ... | @@ -138,8 +163,13 @@ def main(): |
138 | 163 | # --- make list of students to insert/update |
139 | 164 | students = [] |
140 | 165 | |
141 | - for csvfile in args.csvfile: | |
142 | - students += get_students_from_csv(csvfile) | |
166 | + for file in args.files: | |
167 | + if file.endswith('.csv'): | |
168 | + students += get_students_from_csv(file) | |
169 | + elif file.endswith('.xlsx'): | |
170 | + students += get_students_from_xlsx(file) | |
171 | + else: | |
172 | + print(f'!!! Ignoring file {file} !!!') | |
143 | 173 | |
144 | 174 | if args.admin: |
145 | 175 | students.append({'uid': '0', 'name': 'Admin'}) | ... | ... |