From fd2b9fe3d080525fa06112127b5045b375861a7b Mon Sep 17 00:00:00 2001 From: haiyangpengai Date: Wed, 4 Jun 2025 12:58:59 +0800 Subject: [PATCH] fix bugs generated by mypy. --- .../workflow/nodes/document_extractor/node.py | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/api/core/workflow/nodes/document_extractor/node.py b/api/core/workflow/nodes/document_extractor/node.py index e0d75ddab8..429fed2d04 100644 --- a/api/core/workflow/nodes/document_extractor/node.py +++ b/api/core/workflow/nodes/document_extractor/node.py @@ -396,13 +396,13 @@ def _extract_text_from_csv(file_content: bytes) -> str: if not rows: return "" - + # Combine multi-line text in the header row header_row = [cell.replace("\n", " ").replace("\r", "") for cell in rows[0]] # Create Markdown table markdown_table = "| " + " | ".join(header_row) + " |\n" - markdown_table += "| " + " | ".join(["-" * len(col) for col in rows[0]]) + " |\n" + markdown_table += "| " + " | ".join(["-" * len(col) for col in rows[0]]) + " |\n" # Process each data row and combine multi-line text in each cell for row in rows[1:]: @@ -413,25 +413,28 @@ def _extract_text_from_csv(file_content: bytes) -> str: except Exception as e: raise TextExtractionError(f"Failed to extract text from CSV: {str(e)}") from e + def _extract_text_from_excel(file_content: bytes) -> str: """Extract text from an Excel file using pandas.""" + def _construct_markdown_table(df: pd.DataFrame) -> str: """Manually construct a Markdown table from a DataFrame.""" # Construct the header row header_row = "| " + " | ".join(df.columns) + " |" - + # Construct the separator row separator_row = "| " + " | ".join(["-" * len(col) for col in df.columns]) + " |" - + # Construct the data rows data_rows = [] for _, row in df.iterrows(): data_row = "| " + " | ".join(map(str, row)) + " |" data_rows.append(data_row) - + # Combine all rows into a single string markdown_table = "\n".join([header_row, separator_row] + data_rows) return markdown_table + try: excel_file = pd.ExcelFile(io.BytesIO(file_content)) markdown_table = "" @@ -439,13 +442,13 @@ def _extract_text_from_excel(file_content: bytes) -> str: try: df = excel_file.parse(sheet_name=sheet_name) df.dropna(how="all", inplace=True) - + # Combine multi-line text in each cell into a single line - df = df.applymap(lambda x: ' '.join(str(x).splitlines()) if isinstance(x, str) else x) - + df = df.applymap(lambda x: " ".join(str(x).splitlines()) if isinstance(x, str) else x) # type: ignore + # Combine multi-line text in column names into a single line - df.columns = [' '.join(col.splitlines()) for col in df.columns] - + df.columns = pd.Index([" ".join(col.splitlines()) for col in df.columns]) + # Manually construct the Markdown table markdown_table += _construct_markdown_table(df) + "\n\n" except Exception as e: @@ -616,4 +619,4 @@ def _extract_text_from_properties(file_content: bytes) -> str: return "\n".join(result) except Exception as e: - raise TextExtractionError(f"Failed to extract text from properties file: {str(e)}") from e \ No newline at end of file + raise TextExtractionError(f"Failed to extract text from properties file: {str(e)}") from e