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 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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]):
  17. tb += f"<th>{t.value}</th>"
  18. tb += "</tr>"
  19. for r in list(rows[1:]):
  20. tb += "<tr>"
  21. for i, c in enumerate(r):
  22. if c.value is None:
  23. tb += "<td></td>"
  24. else:
  25. tb += f"<td>{c.value}</td>"
  26. tb += "</tr>"
  27. tb += "</table>\n"
  28. return tb
  29. def __call__(self, fnm):
  30. if isinstance(fnm, str):
  31. wb = load_workbook(fnm)
  32. else:
  33. wb = load_workbook(BytesIO(fnm))
  34. res = []
  35. for sheetname in wb.sheetnames:
  36. ws = wb[sheetname]
  37. rows = list(ws.rows)
  38. ti = list(rows[0])
  39. for r in list(rows[1:]):
  40. l = []
  41. for i, c in enumerate(r):
  42. if not c.value:
  43. continue
  44. t = str(ti[i].value) if i < len(ti) else ""
  45. t += (":" if t else "") + str(c.value)
  46. l.append(t)
  47. l = "; ".join(l)
  48. if sheetname.lower().find("sheet") < 0:
  49. l += " ——" + sheetname
  50. res.append(l)
  51. return res
  52. @staticmethod
  53. def row_number(fnm, binary):
  54. if fnm.split(".")[-1].lower().find("xls") >= 0:
  55. wb = load_workbook(BytesIO(binary))
  56. total = 0
  57. for sheetname in wb.sheetnames:
  58. ws = wb[sheetname]
  59. total += len(list(ws.rows))
  60. return total
  61. if fnm.split(".")[-1].lower() in ["csv", "txt"]:
  62. txt = binary.decode("utf-8")
  63. return len(txt.split("\n"))
  64. if __name__ == "__main__":
  65. psr = HuExcelParser()
  66. psr(sys.argv[1])