Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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