Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: excel in node only read one sheet, close #9661 #11215

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions api/core/workflow/nodes/document_extractor/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import docx
import pandas as pd
import pypdfium2
import yaml
import pypdfium2 # type: ignore
import yaml # type: ignore
from unstructured.partition.api import partition_via_api
from unstructured.partition.email import partition_email
from unstructured.partition.epub import partition_epub
Expand Down Expand Up @@ -237,15 +237,17 @@ def _extract_text_from_csv(file_content: bytes) -> str:

def _extract_text_from_excel(file_content: bytes) -> str:
"""Extract text from an Excel file using pandas."""

try:
df = pd.read_excel(io.BytesIO(file_content))

# Drop rows where all elements are NaN
df.dropna(how="all", inplace=True)

# Convert DataFrame to Markdown table
markdown_table = df.to_markdown(index=False)
excel_file = pd.ExcelFile(io.BytesIO(file_content))
markdown_table = ""
for sheet_name in excel_file.sheet_names:
try:
df = excel_file.parse(sheet_name=sheet_name)
df.dropna(how="all", inplace=True)
# Create Markdown table two times to separate tables with a newline
markdown_table += df.to_markdown(index=False) + "\n\n"
except Exception as e:
continue
return markdown_table
except Exception as e:
raise TextExtractionError(f"Failed to extract text from Excel file: {str(e)}") from e
Expand Down