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: skip comment blocks in DOCXToDocument #8764

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions haystack/components/converters/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from docx.document import Document as DocxDocument
from docx.table import Table
from docx.text.paragraph import Paragraph
with LazyImport("Run 'pip install lxml'") as lxml_import:
from lxml.etree import _Comment
Comment on lines +26 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with LazyImport("Run 'pip install lxml'") as lxml_import:
from lxml.etree import _Comment
from lxml.etree import _Comment

Since lxml is a dependency of python-docx (see https://github.com/python-openxml/python-docx/blob/master/requirements.txt), we can safely put this import in the docx_import lazy import block.



@dataclass
Expand Down Expand Up @@ -119,6 +121,7 @@ def __init__(self, table_format: Union[str, DOCXTableFormat] = DOCXTableFormat.C
If False, only the file name is stored.
"""
docx_import.check()
lxml_import.check()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lxml_import.check()

if we incorporate the lxml import in the docx lazy import block, we can remove this line.

self.table_format = DOCXTableFormat.from_str(table_format) if isinstance(table_format, str) else table_format
self.store_full_path = store_full_path

Expand Down Expand Up @@ -210,6 +213,8 @@ def _extract_elements(self, document: "DocxDocument") -> List[str]:
"""
elements = []
for element in document.element.body:
if isinstance(element, _Comment):
continue
if element.tag.endswith("p"):
paragraph = Paragraph(element, document)
if paragraph.contains_page_break:
Expand Down
Loading