You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 1 година
преди 1 година
преди 1 година
преди 1 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. from openpyxl import load_workbook
  3. import sys
  4. from io import BytesIO
  5. class HuExcelParser:
  6. def html(self, fnm):
  7. if isinstance(fnm, str):
  8. wb = load_workbook(fnm)
  9. else:
  10. wb = load_workbook(BytesIO(fnm))
  11. tb = ""
  12. for sheetname in wb.sheetnames:
  13. ws = wb[sheetname]
  14. rows = list(ws.rows)
  15. tb += f"<table><caption>{sheetname}</caption><tr>"
  16. for t in list(rows[0]): tb += f"<th>{t.value}</th>"
  17. tb += "</tr>"
  18. for r in list(rows[1:]):
  19. tb += "<tr>"
  20. for i,c in enumerate(r):
  21. if c.value is None: tb += "<td></td>"
  22. else: tb += f"<td>{c.value}</td>"
  23. tb += "</tr>"
  24. tb += "</table>\n"
  25. return tb
  26. def __call__(self, fnm):
  27. if isinstance(fnm, str):
  28. wb = load_workbook(fnm)
  29. else:
  30. wb = load_workbook(BytesIO(fnm))
  31. res = []
  32. for sheetname in wb.sheetnames:
  33. ws = wb[sheetname]
  34. rows = list(ws.rows)
  35. ti = list(rows[0])
  36. for r in list(rows[1:]):
  37. l = []
  38. for i,c in enumerate(r):
  39. if not c.value:continue
  40. t = str(ti[i].value) if i < len(ti) else ""
  41. t += (":" if t else "") + str(c.value)
  42. l.append(t)
  43. l = "; ".join(l)
  44. if sheetname.lower().find("sheet") <0: l += " ——"+sheetname
  45. res.append(l)
  46. return res
  47. @staticmethod
  48. def row_number(fnm, binary):
  49. if fnm.split(".")[-1].lower().find("xls") >= 0:
  50. wb = load_workbook(BytesIO(binary))
  51. total = 0
  52. for sheetname in wb.sheetnames:
  53. ws = wb[sheetname]
  54. total += len(list(ws.rows))
  55. return total
  56. if fnm.split(".")[-1].lower() in ["csv", "txt"]:
  57. txt = binary.decode("utf-8")
  58. return len(txt.split("\n"))
  59. if __name__ == "__main__":
  60. psr = HuExcelParser()
  61. psr(sys.argv[1])