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 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from openpyxl import load_workbook
  17. import sys
  18. from io import BytesIO
  19. from rag.nlp import find_codec
  20. class RAGFlowExcelParser:
  21. def html(self, fnm, chunk_rows=256):
  22. if isinstance(fnm, str):
  23. wb = load_workbook(fnm)
  24. else:
  25. wb = load_workbook(BytesIO(fnm))
  26. tb_chunks = []
  27. for sheetname in wb.sheetnames:
  28. ws = wb[sheetname]
  29. rows = list(ws.rows)
  30. if not rows:
  31. continue
  32. tb_rows_0 = "<tr>"
  33. for t in list(rows[0]):
  34. tb_rows_0 += f"<th>{t.value}</th>"
  35. tb_rows_0 += "</tr>"
  36. for chunk_i in range((len(rows) - 1) // chunk_rows + 1):
  37. tb = ""
  38. tb += f"<table><caption>{sheetname}</caption>"
  39. tb += tb_rows_0
  40. for r in list(
  41. rows[1 + chunk_i * chunk_rows : 1 + (chunk_i + 1) * chunk_rows]
  42. ):
  43. tb += "<tr>"
  44. for i, c in enumerate(r):
  45. if c.value is None:
  46. tb += "<td></td>"
  47. else:
  48. tb += f"<td>{c.value}</td>"
  49. tb += "</tr>"
  50. tb += "</table>\n"
  51. tb_chunks.append(tb)
  52. return tb_chunks
  53. def __call__(self, fnm):
  54. if isinstance(fnm, str):
  55. wb = load_workbook(fnm)
  56. else:
  57. wb = load_workbook(BytesIO(fnm))
  58. res = []
  59. for sheetname in wb.sheetnames:
  60. ws = wb[sheetname]
  61. rows = list(ws.rows)
  62. if not rows:
  63. continue
  64. ti = list(rows[0])
  65. for r in list(rows[1:]):
  66. fields = []
  67. for i, c in enumerate(r):
  68. if not c.value:
  69. continue
  70. t = str(ti[i].value) if i < len(ti) else ""
  71. t += (":" if t else "") + str(c.value)
  72. fields.append(t)
  73. line = "; ".join(fields)
  74. if sheetname.lower().find("sheet") < 0:
  75. line += " ——" + sheetname
  76. res.append(line)
  77. return res
  78. @staticmethod
  79. def row_number(fnm, binary):
  80. if fnm.split(".")[-1].lower().find("xls") >= 0:
  81. wb = load_workbook(BytesIO(binary))
  82. total = 0
  83. for sheetname in wb.sheetnames:
  84. ws = wb[sheetname]
  85. total += len(list(ws.rows))
  86. return total
  87. if fnm.split(".")[-1].lower() in ["csv", "txt"]:
  88. encoding = find_codec(binary)
  89. txt = binary.decode(encoding, errors="ignore")
  90. return len(txt.split("\n"))
  91. if __name__ == "__main__":
  92. psr = RAGFlowExcelParser()
  93. psr(sys.argv[1])