fix(docx): declare image_data variable when handling pictures (#1359)
Signed-off-by: Cesar Berrospi Ramis <75900930+ceberam@users.noreply.github.com>
This commit is contained in:
parent
250399948d
commit
415b877984
@ -850,7 +850,8 @@ class MsWordDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
def _handle_pictures(
|
def _handle_pictures(
|
||||||
self, docx_obj: DocxDocument, drawing_blip: Any, doc: DoclingDocument
|
self, docx_obj: DocxDocument, drawing_blip: Any, doc: DoclingDocument
|
||||||
) -> None:
|
) -> 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(
|
rId = drawing_blip[0].get(
|
||||||
"{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed"
|
"{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed"
|
||||||
)
|
)
|
||||||
@ -862,19 +863,26 @@ class MsWordDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
|
|
||||||
level = self._get_level()
|
level = self._get_level()
|
||||||
# Open the BytesIO object with PIL to create an Image
|
# Open the BytesIO object with PIL to create an Image
|
||||||
try:
|
image_data: Optional[bytes] = get_docx_image(drawing_blip)
|
||||||
image_data = get_docx_image(drawing_blip)
|
if image_data is None:
|
||||||
image_bytes = BytesIO(image_data)
|
_log.warning("Warning: image cannot be found")
|
||||||
pil_image = Image.open(image_bytes)
|
|
||||||
doc.add_picture(
|
|
||||||
parent=self.parents[level - 1],
|
|
||||||
image=ImageRef.from_pil(image=pil_image, dpi=72),
|
|
||||||
caption=None,
|
|
||||||
)
|
|
||||||
except (UnidentifiedImageError, OSError) as e:
|
|
||||||
_log.warning("Warning: image cannot be loaded by Pillow")
|
|
||||||
doc.add_picture(
|
doc.add_picture(
|
||||||
parent=self.parents[level - 1],
|
parent=self.parents[level - 1],
|
||||||
caption=None,
|
caption=None,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
image_bytes = BytesIO(image_data)
|
||||||
|
pil_image = Image.open(image_bytes)
|
||||||
|
doc.add_picture(
|
||||||
|
parent=self.parents[level - 1],
|
||||||
|
image=ImageRef.from_pil(image=pil_image, dpi=72),
|
||||||
|
caption=None,
|
||||||
|
)
|
||||||
|
except (UnidentifiedImageError, OSError) as e:
|
||||||
|
_log.warning("Warning: image cannot be loaded by Pillow")
|
||||||
|
doc.add_picture(
|
||||||
|
parent=self.parents[level - 1],
|
||||||
|
caption=None,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user