2024-01-15 08:46:22 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from openpyxl import load_workbook
|
|
|
|
|
import sys
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
2024-04-19 18:02:53 +08:00
|
|
|
|
from rag.nlp import find_codec
|
|
|
|
|
|
2024-01-15 08:46:22 +08:00
|
|
|
|
|
2024-04-28 13:19:54 +08:00
|
|
|
|
class RAGFlowExcelParser:
|
2024-05-20 18:35:15 +08:00
|
|
|
|
def html(self, fnm,chunk_rows=256):
|
2024-03-20 16:56:16 +08:00
|
|
|
|
if isinstance(fnm, str):
|
|
|
|
|
wb = load_workbook(fnm)
|
|
|
|
|
else:
|
|
|
|
|
wb = load_workbook(BytesIO(fnm))
|
2024-05-20 18:35:15 +08:00
|
|
|
|
|
|
|
|
|
tb_chunks = []
|
2024-03-20 16:56:16 +08:00
|
|
|
|
for sheetname in wb.sheetnames:
|
|
|
|
|
ws = wb[sheetname]
|
|
|
|
|
rows = list(ws.rows)
|
2024-05-20 18:35:15 +08:00
|
|
|
|
if not rows: continue
|
|
|
|
|
|
|
|
|
|
tb_rows_0 = "<tr>"
|
2024-03-27 11:33:46 +08:00
|
|
|
|
for t in list(rows[0]):
|
2024-05-20 18:35:15 +08:00
|
|
|
|
tb_rows_0 += f"<th>{t.value}</th>"
|
|
|
|
|
tb_rows_0 += "</tr>"
|
|
|
|
|
|
|
|
|
|
for chunk_i in range((len(rows) - 1) // chunk_rows + 1):
|
|
|
|
|
tb = ""
|
|
|
|
|
tb += f"<table><caption>{sheetname}</caption>"
|
|
|
|
|
tb += tb_rows_0
|
|
|
|
|
for r in list(rows[1 + chunk_i * chunk_rows:1 + (chunk_i + 1) * chunk_rows]):
|
|
|
|
|
tb += "<tr>"
|
|
|
|
|
for i, c in enumerate(r):
|
|
|
|
|
if c.value is None:
|
|
|
|
|
tb += "<td></td>"
|
|
|
|
|
else:
|
|
|
|
|
tb += f"<td>{c.value}</td>"
|
|
|
|
|
tb += "</tr>"
|
|
|
|
|
tb += "</table>\n"
|
|
|
|
|
tb_chunks.append(tb)
|
|
|
|
|
|
|
|
|
|
return tb_chunks
|
2024-03-20 16:56:16 +08:00
|
|
|
|
|
2024-01-15 08:46:22 +08:00
|
|
|
|
def __call__(self, fnm):
|
|
|
|
|
if isinstance(fnm, str):
|
|
|
|
|
wb = load_workbook(fnm)
|
|
|
|
|
else:
|
|
|
|
|
wb = load_workbook(BytesIO(fnm))
|
|
|
|
|
res = []
|
|
|
|
|
for sheetname in wb.sheetnames:
|
|
|
|
|
ws = wb[sheetname]
|
|
|
|
|
rows = list(ws.rows)
|
2024-04-02 10:51:21 +08:00
|
|
|
|
if not rows:continue
|
2024-01-15 08:46:22 +08:00
|
|
|
|
ti = list(rows[0])
|
|
|
|
|
for r in list(rows[1:]):
|
|
|
|
|
l = []
|
2024-03-27 11:33:46 +08:00
|
|
|
|
for i, c in enumerate(r):
|
|
|
|
|
if not c.value:
|
|
|
|
|
continue
|
2024-01-15 08:46:22 +08:00
|
|
|
|
t = str(ti[i].value) if i < len(ti) else ""
|
2024-03-12 11:57:08 +08:00
|
|
|
|
t += (":" if t else "") + str(c.value)
|
2024-01-15 08:46:22 +08:00
|
|
|
|
l.append(t)
|
|
|
|
|
l = "; ".join(l)
|
2024-03-27 11:33:46 +08:00
|
|
|
|
if sheetname.lower().find("sheet") < 0:
|
|
|
|
|
l += " ——" + sheetname
|
2024-01-15 08:46:22 +08:00
|
|
|
|
res.append(l)
|
|
|
|
|
return res
|
|
|
|
|
|
2024-03-12 11:57:08 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
def row_number(fnm, binary):
|
|
|
|
|
if fnm.split(".")[-1].lower().find("xls") >= 0:
|
|
|
|
|
wb = load_workbook(BytesIO(binary))
|
|
|
|
|
total = 0
|
|
|
|
|
for sheetname in wb.sheetnames:
|
|
|
|
|
ws = wb[sheetname]
|
2024-03-14 19:45:29 +08:00
|
|
|
|
total += len(list(ws.rows))
|
2024-03-12 11:57:08 +08:00
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
if fnm.split(".")[-1].lower() in ["csv", "txt"]:
|
2024-04-19 18:02:53 +08:00
|
|
|
|
encoding = find_codec(binary)
|
2024-05-07 12:25:47 +08:00
|
|
|
|
txt = binary.decode(encoding, errors="ignore")
|
2024-03-12 11:57:08 +08:00
|
|
|
|
return len(txt.split("\n"))
|
|
|
|
|
|
2024-01-15 08:46:22 +08:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-04-28 13:19:54 +08:00
|
|
|
|
psr = RAGFlowExcelParser()
|
2024-01-15 08:46:22 +08:00
|
|
|
|
psr(sys.argv[1])
|