Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

excel_parser.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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: continue
  28. tb_rows_0 = "<tr>"
  29. for t in list(rows[0]):
  30. tb_rows_0 += f"<th>{t.value}</th>"
  31. tb_rows_0 += "</tr>"
  32. for chunk_i in range((len(rows) - 1) // chunk_rows + 1):
  33. tb = ""
  34. tb += f"<table><caption>{sheetname}</caption>"
  35. tb += tb_rows_0
  36. for r in list(rows[1 + chunk_i * chunk_rows:1 + (chunk_i + 1) * chunk_rows]):
  37. tb += "<tr>"
  38. for i, c in enumerate(r):
  39. if c.value is None:
  40. tb += "<td></td>"
  41. else:
  42. tb += f"<td>{c.value}</td>"
  43. tb += "</tr>"
  44. tb += "</table>\n"
  45. tb_chunks.append(tb)
  46. return tb_chunks
  47. def __call__(self, fnm):
  48. if isinstance(fnm, str):
  49. wb = load_workbook(fnm)
  50. else:
  51. wb = load_workbook(BytesIO(fnm))
  52. res = []
  53. for sheetname in wb.sheetnames:
  54. ws = wb[sheetname]
  55. rows = list(ws.rows)
  56. if not rows:continue
  57. ti = list(rows[0])
  58. for r in list(rows[1:]):
  59. l = []
  60. for i, c in enumerate(r):
  61. if not c.value:
  62. continue
  63. t = str(ti[i].value) if i < len(ti) else ""
  64. t += (":" if t else "") + str(c.value)
  65. l.append(t)
  66. l = "; ".join(l)
  67. if sheetname.lower().find("sheet") < 0:
  68. l += " ——" + sheetname
  69. res.append(l)
  70. return res
  71. @staticmethod
  72. def row_number(fnm, binary):
  73. if fnm.split(".")[-1].lower().find("xls") >= 0:
  74. wb = load_workbook(BytesIO(binary))
  75. total = 0
  76. for sheetname in wb.sheetnames:
  77. ws = wb[sheetname]
  78. total += len(list(ws.rows))
  79. return total
  80. if fnm.split(".")[-1].lower() in ["csv", "txt"]:
  81. encoding = find_codec(binary)
  82. txt = binary.decode(encoding, errors="ignore")
  83. return len(txt.split("\n"))
  84. if __name__ == "__main__":
  85. psr = RAGFlowExcelParser()
  86. psr(sys.argv[1])