Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

excel_parser.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. from openpyxl import load_workbook
  3. import sys
  4. from io import BytesIO
  5. class HuExcelParser:
  6. def __call__(self, fnm):
  7. if isinstance(fnm, str):
  8. wb = load_workbook(fnm)
  9. else:
  10. wb = load_workbook(BytesIO(fnm))
  11. res = []
  12. for sheetname in wb.sheetnames:
  13. ws = wb[sheetname]
  14. rows = list(ws.rows)
  15. ti = list(rows[0])
  16. for r in list(rows[1:]):
  17. l = []
  18. for i,c in enumerate(r):
  19. if not c.value:continue
  20. t = str(ti[i].value) if i < len(ti) else ""
  21. t += (":" if t else "") + str(c.value)
  22. l.append(t)
  23. l = "; ".join(l)
  24. if sheetname.lower().find("sheet") <0: l += " ——"+sheetname
  25. res.append(l)
  26. return res
  27. @staticmethod
  28. def row_number(fnm, binary):
  29. if fnm.split(".")[-1].lower().find("xls") >= 0:
  30. wb = load_workbook(BytesIO(binary))
  31. total = 0
  32. for sheetname in wb.sheetnames:
  33. ws = wb[sheetname]
  34. total += len(list(ws.rows))
  35. return total
  36. if fnm.split(".")[-1].lower() in ["csv", "txt"]:
  37. txt = binary.decode("utf-8")
  38. return len(txt.split("\n"))
  39. if __name__ == "__main__":
  40. psr = HuExcelParser()
  41. psr(sys.argv[1])