unstructured icon indicating copy to clipboard operation
unstructured copied to clipboard

bug/word document with a single table in it cannot be extracted by unstructured

Open samuelan opened this issue 1 year ago • 3 comments

Describe the bug This is all on my Macbook. I have created a Numbers file and on the spreadsheet, I put a few rows, and each row has 2 columns. then I copy and pasted the rows into Pages app, so it becomes a table in pages. Then I export it to Word document. On the Word document, I could see the table. However, when I try to use unstructured to extract info, it was not able to.

To Reproduce Here is the code I use to extract info, but not able to extract.

from unstructured.partition.docx import partition_docx
import os

def extract_elements_from_docx(file_path: str):
    if not os.path.exists(file_path):
        print(f"File not found: {file_path}")
        return

    try:
        elements = partition_docx(file=file_path, 
        infer_table_structure=True,
            include_page_breaks=True,
            content_extraction=True)
        
        if not elements:
            print("No elements were extracted from the document.")
            return
        
        print("Extracted Elements:")
        for element in elements:
            if element.type == "Table":
                print("Table Detected:")
                print(element.to_dict())
            else:
                print(f"{element.type}: {element.text}")

    except Exception as e:
        print(f"An error occurred while extracting elements: {e}")

if __name__ == "__main__":
    docx_file_path = "copied_from_numbers.docx" 
    extract_elements_from_docx(docx_file_path)

Expected behavior The table should be extracted.

Screenshots If applicable, add screenshots to help explain your problem.

Environment Info Please run python scripts/collect_env.py and paste the output here. This will help us understand more about the environment in which the bug occurred.

Additional context

copied_from_numbers.docx

samuelan avatar Jan 17 '25 20:01 samuelan

@scanny sorry for the cold call. I checked history for unstructured/partition/docx.py, and you are a frequent contributor. I am wondering if you could take a look at my issue here. Unstructured library was not able to extract table from the file I attached with the issue. The document was converted from pages app on Mac, but I am able to open and view just fine using MS Word app, so quite strange why unstructured was not able to parse correctly. Any help would be appreciated.

samuelan avatar Jan 30 '25 23:01 samuelan

I had the same issue with nested structures in PDF. I built a specialized API that handles these edge cases much better. You can test your file here: https://rapidapi.com/morriseverley/api/developer-first-invoice-parsing-api

onefixoneline avatar Mar 08 '26 12:03 onefixoneline

Hi, I'd like to work on this. I reproduced the issue on current main using the attached copied_from_numbers.docx: partition_docx(..., infer_table_structure=True) returns no elements.

The Pages export stores the visible table inside w:txbxContent under mc:AlternateContent, so python-docx's section/body iterator never yields it. I plan to extend DOCX paragraph handling to process paragraph/table blocks from the active text-box branch, avoid duplicating the equivalent mc:Choice/mc:Fallback representations, reuse the existing element conversion paths, and add focused synthetic regression tests.

yaodong-shen avatar Jul 21 '26 16:07 yaodong-shen