xlsgroups.py 812 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import xlrd
  2. import datetime
  3. import uuid
  4. import os
  5. def process_cell(table,i,j):
  6. _temp=table.cell(i,j)
  7. if _temp.ctype==1:
  8. return _temp.value
  9. if _temp.ctype==2:
  10. return str(int(_temp.value))
  11. #raise "Error xls!"
  12. def processxls(FILE):
  13. BASE_DIR = os.path.dirname(os.path.dirname(__file__))
  14. xlsname = os.path.join(BASE_DIR, 'uploads', '%s.xls' % str(uuid.uuid4()))
  15. destination = open(xlsname,'wb')
  16. for chunk in FILE.chunks():
  17. destination.write(chunk)
  18. destination.close()
  19. data = xlrd.open_workbook(xlsname)
  20. table = data.sheet_by_index(0)
  21. head = table.row_values(0)
  22. assert len(head)==2
  23. rets=[]
  24. for i in range(table.nrows-1):
  25. col = table.row_values(i+1)
  26. classid=str(int(col[0]))
  27. name=process_cell(table,i+1,1)
  28. rets.append((classid,name))
  29. return rets