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

feat: include text from shapes in docx #2510

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* **Add .heic file partitioning** .heic image files were previously unsupported and are now supported though partition_image()
* **Add the ability to specify an alternate OCR** implementation by implementing an `OCRAgent` interface and specify it using `OCR_AGENT` environment variable.
* **Add Vectara destination connector** Adds support for writing partitioned documents into a Vectara index.
* **Add ability to detect text in .docx inline shapes** extensions of docx partition, extracts text from inline shapes and includes them in paragraph's text

### Fixes

Expand All @@ -37,6 +38,7 @@
* **Add title to Vectara upload - was not separated out from initial connector **
* **Fix change OpenSearch port to fix potential conflict with Elasticsearch in ingest test **


ds-filipknefel marked this conversation as resolved.
Show resolved Hide resolved
## 0.12.3

### Enhancements
Expand Down
Binary file added example-docs/docx-shapes.docx
Binary file not shown.
14 changes: 14 additions & 0 deletions test_unstructured/partition/docx/test_docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,20 @@ def test_partition_docx_includes_hyperlink_metadata():
assert metadata.link_urls is None


# -- shape behaviors -----------------------------------------------------------------------------


def test_it_considers_text_inside_shapes():
# -- <bracketed> text is written inside inline shapes --
partitioned_doc = partition_docx(example_doc_path("docx-shapes.docx"))
assert [element.text for element in partitioned_doc] == [
"Paragraph with single <inline-image> within.",
"Paragraph with <inline-image1> and <inline-image2> within.",
# -- text "<floating-shape>" in floating shape is ignored --
"Paragraph with floating shape attached.",
]


# -- module-level fixtures -----------------------------------------------------------------------


Expand Down
7 changes: 6 additions & 1 deletion unstructured/partition/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ def _classify_paragraph_to_element(self, paragraph: Paragraph) -> Iterator[Eleme
does not contribute to the document-element stream and will not cause an element to be
emitted.
"""
text = paragraph.text
text = "".join(
e.text
for e in paragraph._p.xpath(
"w:r | w:hyperlink | w:r/descendant::wp:inline[ancestor::w:drawing][1]//w:r"
)
)

# NOTE(scanny) - blank paragraphs are commonly used for spacing between paragraphs and
# do not contribute to the document-element stream.
Expand Down
Loading