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: Fixed docx import with headers that are also lists #842

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 22 additions & 8 deletions docling/backend/msword_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ def handle_text_elements(self, element, docx_obj, doc):
numid = None

# Handle lists
if numid is not None and ilevel is not None:
if (
numid is not None
and ilevel is not None
and p_style_id not in ["Title", "Heading"]
):
self.add_listitem(
element,
docx_obj,
Expand All @@ -254,12 +258,22 @@ def handle_text_elements(self, element, docx_obj, doc):
)
self.update_history(p_style_id, p_level, numid, ilevel)
return
elif numid is None and self.prev_numid() is not None: # Close list
for key, val in self.parents.items():
if key >= self.level_at_new_list:
elif (
numid is None
and self.prev_numid() is not None
and p_style_id not in ["Title", "Heading"]
): # Close list
if self.level_at_new_list:
for key, val in self.parents.items():
maxmnemonic marked this conversation as resolved.
Show resolved Hide resolved
if key >= self.level_at_new_list:
self.parents[key] = None
self.level = self.level_at_new_list - 1
self.level_at_new_list = None
else:
for key, val in self.parents.items():
maxmnemonic marked this conversation as resolved.
Show resolved Hide resolved
self.parents[key] = None
self.level = self.level_at_new_list - 1
self.level_at_new_list = None
self.level = 0

if p_style_id in ["Title"]:
for key, val in self.parents.items():
self.parents[key] = None
Expand Down Expand Up @@ -520,11 +534,11 @@ def get_docx_image(element, drawing_blip):
image_data = image_part.blob # Get the binary image data
return image_data

image_data = get_docx_image(element, drawing_blip)
image_bytes = BytesIO(image_data)
level = self.get_level()
# Open the BytesIO object with PIL to create an Image
try:
image_data = get_docx_image(element, drawing_blip)
image_bytes = BytesIO(image_data)
pil_image = Image.open(image_bytes)
doc.add_picture(
parent=self.parents[level - 1],
Expand Down
Binary file added tests/data/docx/unit_test_headers_numbered.docx
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
item-0 at level 0: unspecified: group _root_
item-1 at level 1: title: Test Document
item-2 at level 2: paragraph:
item-3 at level 2: section_header: Section 1
item-4 at level 1: paragraph:
item-5 at level 1: paragraph: Paragraph 1.1
item-6 at level 1: paragraph:
item-7 at level 1: paragraph: Paragraph 1.2
item-8 at level 1: paragraph:
item-9 at level 1: section: group header-0
item-10 at level 2: section: group header-1
item-11 at level 3: section_header: Section 1.1
item-12 at level 4: paragraph:
item-13 at level 4: paragraph: Paragraph 1.1.1
item-14 at level 4: paragraph:
item-15 at level 4: paragraph: Paragraph 1.1.2
item-16 at level 4: paragraph:
item-17 at level 3: section_header: Section 1.2
item-18 at level 4: paragraph:
item-19 at level 4: paragraph: Paragraph 1.1.1
item-20 at level 4: paragraph:
item-21 at level 4: paragraph: Paragraph 1.1.2
item-22 at level 4: paragraph:
item-23 at level 4: section_header: Section 1.2.3
item-24 at level 5: paragraph:
item-25 at level 5: paragraph: Paragraph 1.2.3.1
item-26 at level 5: paragraph:
item-27 at level 5: paragraph: Paragraph 1.2.3.1
item-28 at level 5: paragraph:
item-29 at level 5: paragraph:
item-30 at level 2: section_header: Section 2
item-31 at level 1: paragraph:
item-32 at level 1: paragraph: Paragraph 2.1
item-33 at level 1: paragraph:
item-34 at level 1: paragraph: Paragraph 2.2
item-35 at level 1: paragraph:
item-36 at level 1: section: group header-0
item-37 at level 2: section: group header-1
item-38 at level 3: section: group header-2
item-39 at level 4: section_header: Section 2.1.1
item-40 at level 5: paragraph:
item-41 at level 5: paragraph: Paragraph 2.1.1.1
item-42 at level 5: paragraph:
item-43 at level 5: paragraph: Paragraph 2.1.1.1
item-44 at level 5: paragraph:
item-45 at level 3: section_header: Section 2.1
item-46 at level 4: paragraph:
item-47 at level 4: paragraph: Paragraph 2.1.1
item-48 at level 4: paragraph:
item-49 at level 4: paragraph: Paragraph 2.1.2
item-50 at level 4: paragraph:
item-51 at level 4: paragraph:
Loading