excel_checker.py 726 B

1234567891011121314151617181920212223242526
  1. import xlrd
  2. from common.exceptions import ExcelFileError
  3. def check_excel_file(file):
  4. sheets_names = ['oferta', 'existentes', 'mct', 'rtr', 'exp']
  5. try:
  6. xls = xlrd.open_workbook(file, on_demand=True)
  7. sheets = xls.sheet_names()
  8. except Exception:
  9. raise ExcelFileError(file)
  10. else:
  11. not_found = []
  12. for sheet in sheets_names:
  13. sheet_exists = sheet in sheets
  14. if not sheet_exists:
  15. not_found.append(sheet)
  16. if not_found:
  17. msg = 'El archivo no contiene la(s) hoja(s) '
  18. msg += f'<{", ".join(sheet for sheet in not_found)}>'
  19. raise ExcelFileError(
  20. file,
  21. message=msg)