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.

excel_parser.py 3.3KB

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