 Fix "File contains no valid workbook part" (#9360)
### What problem does this PR solve?
fix "File contains no valid workbook part"
stacktrace:
```
Traceback (most recent call last):
File "/ragflow/deepdoc/parser/excel_parser.py", line 54, in _load_excel_to_workbook
return RAGFlowExcelParser._dataframe_to_workbook(df)
File "/ragflow/deepdoc/parser/excel_parser.py", line 69, in _dataframe_to_workbook
ws.cell(row=row_num, column=col_num, value=value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/worksheet/worksheet.py", line 246, in cell
cell.value = value
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 218, in value
self._bind_value(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 197, in _bind_value
value = self.check_string(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 165, in check_string
raise IllegalCharacterError(f"{value} cannot be used in worksheets.")
```
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe): 2 miesięcy temu  Fix "File contains no valid workbook part" (#9360)
### What problem does this PR solve?
fix "File contains no valid workbook part"
stacktrace:
```
Traceback (most recent call last):
File "/ragflow/deepdoc/parser/excel_parser.py", line 54, in _load_excel_to_workbook
return RAGFlowExcelParser._dataframe_to_workbook(df)
File "/ragflow/deepdoc/parser/excel_parser.py", line 69, in _dataframe_to_workbook
ws.cell(row=row_num, column=col_num, value=value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/worksheet/worksheet.py", line 246, in cell
cell.value = value
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 218, in value
self._bind_value(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 197, in _bind_value
value = self.check_string(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 165, in check_string
raise IllegalCharacterError(f"{value} cannot be used in worksheets.")
```
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe): 2 miesięcy temu  Add fallback to use 'calamine' parse engine in excel_parser.py (#9374)
### What problem does this PR solve?
add fallback to `calamine` engine when parse error raised using the
default `openpyxl` / `xlrd` engine.
e.g. the following error can be fixed:
```
Traceback (most recent call last):
File "/ragflow/deepdoc/parser/excel_parser.py", line 53, in _load_excel_to_workbook
df = pd.read_excel(file_like_object)
File "/ragflow/.venv/lib/python3.10/site-packages/pandas/io/excel/_base.py", line 495, in read_excel
io = ExcelFile(
File "/ragflow/.venv/lib/python3.10/site-packages/pandas/io/excel/_base.py", line 1567, in __init__
self._reader = self._engines[engine](
File "/ragflow/.venv/lib/python3.10/site-packages/pandas/io/excel/_xlrd.py", line 46, in __init__
super().__init__(
File "/ragflow/.venv/lib/python3.10/site-packages/pandas/io/excel/_base.py", line 573, in __init__
self.book = self.load_workbook(self.handles.handle, engine_kwargs)
File "/ragflow/.venv/lib/python3.10/site-packages/pandas/io/excel/_xlrd.py", line 63, in load_workbook
return open_workbook(file_contents=data, **engine_kwargs)
File "/ragflow/.venv/lib/python3.10/site-packages/xlrd/__init__.py", line 172, in open_workbook
bk = open_workbook_xls(
File "/ragflow/.venv/lib/python3.10/site-packages/xlrd/book.py", line 68, in open_workbook_xls
bk.biff2_8_load(
File "/ragflow/.venv/lib/python3.10/site-packages/xlrd/book.py", line 641, in biff2_8_load
cd.locate_named_stream(UNICODE_LITERAL(qname))
File "/ragflow/.venv/lib/python3.10/site-packages/xlrd/compdoc.py", line 398, in locate_named_stream
result = self._locate_stream(
File "/ragflow/.venv/lib/python3.10/site-packages/xlrd/compdoc.py", line 429, in _locate_stream
raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s]))
xlrd.compdoc.CompDocError: Workbook corruption: seen[2] == 4
```
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue) 2 miesięcy temu  Fix "File contains no valid workbook part" (#9360)
### What problem does this PR solve?
fix "File contains no valid workbook part"
stacktrace:
```
Traceback (most recent call last):
File "/ragflow/deepdoc/parser/excel_parser.py", line 54, in _load_excel_to_workbook
return RAGFlowExcelParser._dataframe_to_workbook(df)
File "/ragflow/deepdoc/parser/excel_parser.py", line 69, in _dataframe_to_workbook
ws.cell(row=row_num, column=col_num, value=value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/worksheet/worksheet.py", line 246, in cell
cell.value = value
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 218, in value
self._bind_value(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 197, in _bind_value
value = self.check_string(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 165, in check_string
raise IllegalCharacterError(f"{value} cannot be used in worksheets.")
```
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe): 2 miesięcy temu  Fix "File contains no valid workbook part" (#9360)
### What problem does this PR solve?
fix "File contains no valid workbook part"
stacktrace:
```
Traceback (most recent call last):
File "/ragflow/deepdoc/parser/excel_parser.py", line 54, in _load_excel_to_workbook
return RAGFlowExcelParser._dataframe_to_workbook(df)
File "/ragflow/deepdoc/parser/excel_parser.py", line 69, in _dataframe_to_workbook
ws.cell(row=row_num, column=col_num, value=value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/worksheet/worksheet.py", line 246, in cell
cell.value = value
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 218, in value
self._bind_value(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 197, in _bind_value
value = self.check_string(value)
File "/ragflow/.venv/lib/python3.10/site-packages/openpyxl/cell/cell.py", line 165, in check_string
raise IllegalCharacterError(f"{value} cannot be used in worksheets.")
```
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe): 2 miesięcy temu |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
-
- import logging
- import re
- import sys
- from io import BytesIO
-
- import pandas as pd
- from openpyxl import Workbook, load_workbook
-
- from rag.nlp import find_codec
-
- # copied from `/openpyxl/cell/cell.py`
- ILLEGAL_CHARACTERS_RE = re.compile(r'[\000-\010]|[\013-\014]|[\016-\037]')
-
- class RAGFlowExcelParser:
-
- @staticmethod
- def _load_excel_to_workbook(file_like_object):
- if isinstance(file_like_object, bytes):
- file_like_object = BytesIO(file_like_object)
-
- # Read first 4 bytes to determine file type
- file_like_object.seek(0)
- file_head = file_like_object.read(4)
- file_like_object.seek(0)
-
- if not (file_head.startswith(b'PK\x03\x04') or file_head.startswith(b'\xD0\xCF\x11\xE0')):
- logging.info("Not an Excel file, converting CSV to Excel Workbook")
-
- try:
- file_like_object.seek(0)
- df = pd.read_csv(file_like_object)
- return RAGFlowExcelParser._dataframe_to_workbook(df)
-
- except Exception as e_csv:
- raise Exception(f"Failed to parse CSV and convert to Excel Workbook: {e_csv}")
-
- try:
- return load_workbook(file_like_object,data_only= True)
- except Exception as e:
- logging.info(f"openpyxl load error: {e}, try pandas instead")
- try:
- file_like_object.seek(0)
- try:
- df = pd.read_excel(file_like_object)
- return RAGFlowExcelParser._dataframe_to_workbook(df)
- except Exception as ex:
- logging.info(f"pandas with default engine load error: {ex}, try calamine instead")
- file_like_object.seek(0)
- df = pd.read_excel(file_like_object, engine='calamine')
- return RAGFlowExcelParser._dataframe_to_workbook(df)
- except Exception as e_pandas:
- raise Exception(f"pandas.read_excel error: {e_pandas}, original openpyxl error: {e}")
-
- @staticmethod
- def _clean_dataframe(df: pd.DataFrame):
- def clean_string(s):
- if isinstance(s, str):
- return ILLEGAL_CHARACTERS_RE.sub(" ", s)
- return s
-
- return df.apply(lambda col: col.map(clean_string))
-
- @staticmethod
- def _dataframe_to_workbook(df):
- df = RAGFlowExcelParser._clean_dataframe(df)
- wb = Workbook()
- ws = wb.active
- ws.title = "Data"
-
- for col_num, column_name in enumerate(df.columns, 1):
- ws.cell(row=1, column=col_num, value=column_name)
-
- for row_num, row in enumerate(df.values, 2):
- for col_num, value in enumerate(row, 1):
- ws.cell(row=row_num, column=col_num, value=value)
-
- return wb
-
- def html(self, fnm, chunk_rows=256):
- file_like_object = BytesIO(fnm) if not isinstance(fnm, str) else fnm
- wb = RAGFlowExcelParser._load_excel_to_workbook(file_like_object)
- tb_chunks = []
- for sheetname in wb.sheetnames:
- ws = wb[sheetname]
- rows = list(ws.rows)
- if not rows:
- continue
-
- tb_rows_0 = "<tr>"
- for t in list(rows[0]):
- 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
-
- def __call__(self, fnm):
- file_like_object = BytesIO(fnm) if not isinstance(fnm, str) else fnm
- wb = RAGFlowExcelParser._load_excel_to_workbook(file_like_object)
-
- res = []
- for sheetname in wb.sheetnames:
- ws = wb[sheetname]
- rows = list(ws.rows)
- if not rows:
- continue
- ti = list(rows[0])
- for r in list(rows[1:]):
- fields = []
- for i, c in enumerate(r):
- if not c.value:
- continue
- t = str(ti[i].value) if i < len(ti) else ""
- t += (":" if t else "") + str(c.value)
- fields.append(t)
- line = "; ".join(fields)
- if sheetname.lower().find("sheet") < 0:
- line += " ——" + sheetname
- res.append(line)
- return res
-
- @staticmethod
- def row_number(fnm, binary):
- if fnm.split(".")[-1].lower().find("xls") >= 0:
- wb = RAGFlowExcelParser._load_excel_to_workbook(BytesIO(binary))
- total = 0
- for sheetname in wb.sheetnames:
- ws = wb[sheetname]
- total += len(list(ws.rows))
- return total
-
- if fnm.split(".")[-1].lower() in ["csv", "txt"]:
- encoding = find_codec(binary)
- txt = binary.decode(encoding, errors="ignore")
- return len(txt.split("\n"))
-
-
- if __name__ == "__main__":
- psr = RAGFlowExcelParser()
- psr(sys.argv[1])
|