The text data in the tables of the Word file cannot be extracted #6632

Closed
opened 2026-02-21 18:16:38 -05:00 by yindo · 6 comments
Owner

Originally created by @naked34501 on GitHub (Nov 8, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.11.0

Cloud or Self Hosted

Cloud, Self Hosted (Docker)

Steps to reproduce

In Issue #10359 , I mentioned a problem with extracting tables from Word documents. You responded that it was working as expected. However, I don’t have permission to reopen the issue. I may not have described the problem clearly: the issue occurs with the document extractor feature in the workflow’s document upload function, not in the knowledge base. The extractor cannot read my document. Could you please test this again? Thank you!

✔️ Expected Behavior

30万元

Actual Behavior


Originally created by @naked34501 on GitHub (Nov 8, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.11.0 ### Cloud or Self Hosted Cloud, Self Hosted (Docker) ### Steps to reproduce In Issue #10359 , I mentioned a problem with extracting tables from Word documents. You responded that it was working as expected. However, I don’t have permission to reopen the issue. I may not have described the problem clearly: the issue occurs with the document extractor feature in the workflow’s document upload function, not in the knowledge base. The extractor cannot read my document. Could you please test this again? Thank you! ### ✔️ Expected Behavior 30万元 ### ❌ Actual Behavior ---
yindo added the 🐞 bug label 2026-02-21 18:16:38 -05:00
yindo closed this issue 2026-02-21 18:16:38 -05:00
Author
Owner

@naked34501 commented on GitHub (Nov 8, 2024):

here is the doc
测试改合同.docx

@naked34501 commented on GitHub (Nov 8, 2024): here is the doc [测试改合同.docx](https://github.com/user-attachments/files/17675728/default.docx)
Author
Owner

@dosubot[bot] commented on GitHub (Nov 8, 2024):

I found a similar issue that was previously reported and closed. The issue was related to the inability to extract text data from tables in a Word file using Dify version 0.11.0. The suggested solution involves ensuring the correct use of the WordExtractor class, which includes a method _table_to_markdown to convert tables in a Word document to Markdown format. Here’s a brief guide on how to address this:

  1. Initialize the WordExtractor: Create an instance of the WordExtractor class by providing the file path, tenant ID, and user ID.

  2. Extract Content: Call the extract method, which processes the Word document and converts tables to Markdown.

  3. Check Dependencies: Ensure that all necessary dependencies, such as the unstructured library (version 0.4.11 or higher), are installed, as this library is crucial for processing Word documents.

If the issue persists, verify that the Word document is not corrupted and that the tables are formatted in a way that the extractor can process. If the problem continues, there might be a specific issue with the document structure that needs further investigation [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Nov 8, 2024): <!-- Answer --> I found a similar issue that was previously reported and closed. The issue was related to the inability to extract text data from tables in a Word file using Dify version 0.11.0. The suggested solution involves ensuring the correct use of the `WordExtractor` class, which includes a method `_table_to_markdown` to convert tables in a Word document to Markdown format. Here’s a brief guide on how to address this: 1. **Initialize the `WordExtractor`:** Create an instance of the `WordExtractor` class by providing the file path, tenant ID, and user ID. 2. **Extract Content:** Call the `extract` method, which processes the Word document and converts tables to Markdown. 3. **Check Dependencies:** Ensure that all necessary dependencies, such as the `unstructured` library (version 0.4.11 or higher), are installed, as this library is crucial for processing Word documents. If the issue persists, verify that the Word document is not corrupted and that the tables are formatted in a way that the extractor can process. If the problem continues, there might be a specific issue with the document structure that needs further investigation <sup>[[1]](https://github.com/langgenius/dify/issues/10359)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@naked34501 commented on GitHub (Dec 5, 2024):

PLS CHECK THIS BUG

@naked34501 commented on GitHub (Dec 5, 2024): PLS CHECK THIS BUG
Author
Owner

@akinobu-i commented on GitHub (Dec 19, 2024):

I have the same request as well. "The text data in the tables of the Word file cannot be extracted"

Is the following part of the source code relevant to this issue?
https://github.com/langgenius/dify/blob/463fbe268047520fba99b60c123a88a4c5141884/api/core/workflow/nodes/document_extractor/node.py#L185C1-L191C92

The minimum solution I hope for is to retrieve all the text from each table and return it as part of the output, as shown below. (Please forgive me if there are any mistakes, as I am not a programmer.)

from docx.oxml.text.paragraph import CT_P
from docx.oxml.table import CT_Tbl
from docx.text.paragraph import Paragraph
from docx.table import Table

def extract_text_from_doc(file_content: bytes) -> str:
    try:
        doc_file = io.BytesIO(file_content)
        doc = docx.Document(doc_file)
        
        result = []
        previous_was_table = False
        
        for element in doc.element.body:
            if isinstance(element, CT_P):
                paragraph = Paragraph(element, doc)
                if paragraph.text.strip():
                    # If previous element was a table, add empty line
                    if previous_was_table:
                        result.append('')
                    result.append(paragraph.text.strip())
                    previous_was_table = False
                    
            elif isinstance(element, CT_Tbl):
                # Add empty line before table if previous element wasn't a table
                if not previous_was_table and result:
                    result.append('')
                    
                table = Table(element, doc)
                for row in table.rows:
                    row_text = [cell.text.strip() for cell in row.cells]
                    result.append('\t'.join(row_text))
                
                previous_was_table = True
        
        return '\n'.join(result)
        
    except Exception as e:
        raise TextExtractionError(f"Failed to extract text from DOC/DOCX: {str(e)}") from e
@akinobu-i commented on GitHub (Dec 19, 2024): I have the same request as well. "The text data in the tables of the Word file cannot be extracted" Is the following part of the source code relevant to this issue? https://github.com/langgenius/dify/blob/463fbe268047520fba99b60c123a88a4c5141884/api/core/workflow/nodes/document_extractor/node.py#L185C1-L191C92 The minimum solution I hope for is to retrieve all the text from each table and return it as part of the output, as shown below. (Please forgive me if there are any mistakes, as I am not a programmer.) ``` from docx.oxml.text.paragraph import CT_P from docx.oxml.table import CT_Tbl from docx.text.paragraph import Paragraph from docx.table import Table def extract_text_from_doc(file_content: bytes) -> str: try: doc_file = io.BytesIO(file_content) doc = docx.Document(doc_file) result = [] previous_was_table = False for element in doc.element.body: if isinstance(element, CT_P): paragraph = Paragraph(element, doc) if paragraph.text.strip(): # If previous element was a table, add empty line if previous_was_table: result.append('') result.append(paragraph.text.strip()) previous_was_table = False elif isinstance(element, CT_Tbl): # Add empty line before table if previous element wasn't a table if not previous_was_table and result: result.append('') table = Table(element, doc) for row in table.rows: row_text = [cell.text.strip() for cell in row.cells] result.append('\t'.join(row_text)) previous_was_table = True return '\n'.join(result) except Exception as e: raise TextExtractionError(f"Failed to extract text from DOC/DOCX: {str(e)}") from e ```
Author
Owner

@yihong0618 commented on GitHub (Dec 19, 2024):

I have the same request as well. "The text data in the tables of the Word file cannot be extracted"

Is the following part of the source code relevant to this issue? https://github.com/langgenius/dify/blob/463fbe268047520fba99b60c123a88a4c5141884/api/core/workflow/nodes/document_extractor/node.py#L185C1-L191C92

The minimum solution I hope for is to retrieve all the text from each table and return it as part of the output, as shown below. (Please forgive me if there are any mistakes, as I am not a programmer.)

from docx.oxml.text.paragraph import CT_P
from docx.oxml.table import CT_Tbl
from docx.text.paragraph import Paragraph
from docx.table import Table

def extract_text_from_doc(file_content: bytes) -> str:
    try:
        doc_file = io.BytesIO(file_content)
        doc = docx.Document(doc_file)
        
        result = []
        previous_was_table = False
        
        for element in doc.element.body:
            if isinstance(element, CT_P):
                paragraph = Paragraph(element, doc)
                if paragraph.text.strip():
                    # If previous element was a table, add empty line
                    if previous_was_table:
                        result.append('')
                    result.append(paragraph.text.strip())
                    previous_was_table = False
                    
            elif isinstance(element, CT_Tbl):
                # Add empty line before table if previous element wasn't a table
                if not previous_was_table and result:
                    result.append('')
                    
                table = Table(element, doc)
                for row in table.rows:
                    row_text = [cell.text.strip() for cell in row.cells]
                    result.append('\t'.join(row_text))
                
                previous_was_table = True
        
        return '\n'.join(result)
        
    except Exception as e:
        raise TextExtractionError(f"Failed to extract text from DOC/DOCX: {str(e)}") from e

Thanks will take a look today

@yihong0618 commented on GitHub (Dec 19, 2024): > I have the same request as well. "The text data in the tables of the Word file cannot be extracted" > > Is the following part of the source code relevant to this issue? https://github.com/langgenius/dify/blob/463fbe268047520fba99b60c123a88a4c5141884/api/core/workflow/nodes/document_extractor/node.py#L185C1-L191C92 > > The minimum solution I hope for is to retrieve all the text from each table and return it as part of the output, as shown below. (Please forgive me if there are any mistakes, as I am not a programmer.) > > ``` > from docx.oxml.text.paragraph import CT_P > from docx.oxml.table import CT_Tbl > from docx.text.paragraph import Paragraph > from docx.table import Table > > def extract_text_from_doc(file_content: bytes) -> str: > try: > doc_file = io.BytesIO(file_content) > doc = docx.Document(doc_file) > > result = [] > previous_was_table = False > > for element in doc.element.body: > if isinstance(element, CT_P): > paragraph = Paragraph(element, doc) > if paragraph.text.strip(): > # If previous element was a table, add empty line > if previous_was_table: > result.append('') > result.append(paragraph.text.strip()) > previous_was_table = False > > elif isinstance(element, CT_Tbl): > # Add empty line before table if previous element wasn't a table > if not previous_was_table and result: > result.append('') > > table = Table(element, doc) > for row in table.rows: > row_text = [cell.text.strip() for cell in row.cells] > result.append('\t'.join(row_text)) > > previous_was_table = True > > return '\n'.join(result) > > except Exception as e: > raise TextExtractionError(f"Failed to extract text from DOC/DOCX: {str(e)}") from e > ``` Thanks will take a look today
Author
Owner

@akinobu-i commented on GitHub (Dec 19, 2024):

( Of course, having the text inside the table expanded in Markdown format would be a better solution. )

@akinobu-i commented on GitHub (Dec 19, 2024): ( Of course, having the text inside the table expanded in Markdown format would be a better solution. )
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6632