xlsadd.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: UTF-8 -*-
  2. import xlrd
  3. import datetime
  4. import uuid
  5. import os
  6. def process_cell(table,i,j):
  7. _temp=table.cell(i,j)
  8. if _temp.ctype==1:
  9. return _temp.value
  10. if _temp.ctype==2:
  11. return str(int(_temp.value))
  12. #raise "Error xls!"
  13. def processPWXLS(FILE):
  14. BASE_DIR = os.path.dirname(os.path.dirname(__file__))
  15. xlsname = os.path.join(BASE_DIR, 'uploads', '%s.xls' % str(uuid.uuid4()))
  16. destination = open(xlsname,'wb')
  17. for chunk in FILE.chunks():
  18. destination.write(chunk)
  19. destination.close()
  20. data = xlrd.open_workbook(xlsname)
  21. table = data.sheet_by_index(0)
  22. head = table.row_values(0)
  23. assert len(head)==6
  24. assert head[0].strip()==u'作品ID'
  25. assert head[1].strip()==u'参赛者用户名'
  26. assert head[2].strip()==u'作品标题'
  27. assert head[3].strip()==u'作品描述'
  28. assert head[4].strip()==u'文件名'
  29. assert head[5].strip()==u'文件扩展名'
  30. rets=[]
  31. for i in range(table.nrows-1):
  32. col = table.row_values(i+1)
  33. uuid = col[0]
  34. user = col[1]
  35. title = col[2]
  36. desc = col[3]
  37. filename = col[4]
  38. fileext = col[5]
  39. rets.append((uuid,user,title,desc,filename,fileext))
  40. return rets