fix(docx): declare image_data variable when handling pictures

Signed-off-by: Cesar Berrospi Ramis <75900930+ceberam@users.noreply.github.com>
This commit is contained in:
Cesar Berrospi Ramis 2025-04-11 10:58:15 +02:00
parent eef2bdea77
commit 4b0e3ef104

View File

@ -850,7 +850,8 @@ class MsWordDocumentBackend(DeclarativeDocumentBackend):
def _handle_pictures(
self, docx_obj: DocxDocument, drawing_blip: Any, doc: DoclingDocument
) -> None:
def get_docx_image(drawing_blip):
def get_docx_image(drawing_blip: Any) -> Optional[bytes]:
image_data: Optional[bytes] = None
rId = drawing_blip[0].get(
"{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed"
)
@ -862,8 +863,15 @@ class MsWordDocumentBackend(DeclarativeDocumentBackend):
level = self._get_level()
# Open the BytesIO object with PIL to create an Image
image_data: Optional[bytes] = get_docx_image(drawing_blip)
if image_data is None:
_log.warning("Warning: image cannot be found")
doc.add_picture(
parent=self.parents[level - 1],
caption=None,
)
else:
try:
image_data = get_docx_image(drawing_blip)
image_bytes = BytesIO(image_data)
pil_image = Image.open(image_bytes)
doc.add_picture(