excel_output.py 873 B

123456789101112131415161718192021222324252627282930313233
  1. # coding:utf-8
  2. import StringIO
  3. import xlwt
  4. import zipfile
  5. import datetime
  6. import random, string
  7. import os
  8. from ftplib import FTP
  9. def save2memory(en_cn_map, content):
  10. """
  11. 保存内容到xls里面
  12. :param en_cn_map: 英文中文对应字典 {"name": "名字"}, OrderedDict
  13. :param content:
  14. :return:
  15. """
  16. wb = xlwt.Workbook(encoding="utf-8")
  17. sheet = wb.add_sheet('info')
  18. sort_head = en_cn_map.keys()
  19. for i, item in enumerate(en_cn_map):
  20. sheet.write(0, i, en_cn_map.get(item))
  21. for row, item in enumerate(content):
  22. sort_key = item.keys()
  23. for key in sort_key:
  24. if key not in sort_head:
  25. continue
  26. sheet.write(row+1, sort_head.index(key), item.get(key, ''))
  27. output = StringIO.StringIO()
  28. wb.save(output)
  29. return output.getvalue()
  30. if __name__ == '__main__':
  31. pass