diff --git a/test_unstructured/partition/test_pptx_emphasis.py b/test_unstructured/partition/test_pptx_emphasis.py new file mode 100644 index 0000000000..a030bffd85 --- /dev/null +++ b/test_unstructured/partition/test_pptx_emphasis.py @@ -0,0 +1,55 @@ +import io + +import pptx +import pytest +from pptx.util import Inches + +from unstructured.partition.pptx import partition_pptx + + +@pytest.mark.parametrize("title", [False, True]) +def test_partition_preserves_run_and_paragraph_emphasis(title): + presentation = pptx.Presentation() + slide = presentation.slides.add_slide(presentation.slide_layouts[0 if title else 6]) + shape = ( + slide.shapes.title + if title + else slide.shapes.add_textbox(Inches(1), Inches(1), Inches(8), Inches(2)) + ) + paragraph = shape.text_frame.paragraphs[0] + paragraph.font.bold = True + for text, bold, italic in [ + (" Inherited ", None, None), + ("plain ", False, None), + ("both ", True, True), + ("italic ", False, True), + (" ", True, True), + ]: + run = paragraph.add_run() + run.text = text + run.font.bold = bold + run.font.italic = italic + file = io.BytesIO() + presentation.save(file) + + elements = partition_pptx(file=file, languages=["eng"]) + + element = next(e for e in elements if "Inherited" in e.text) + assert element.metadata.emphasized_text_contents == ["Inherited", "both", "both", "italic"] + assert element.metadata.emphasized_text_tags == ["b", "b", "i", "i"] + assert element.text == " Inherited plain both italic " + assert element.metadata.page_number == 1 + + +def test_unformatted_presentation_omits_emphasis_metadata(): + presentation = pptx.Presentation() + slide = presentation.slides.add_slide(presentation.slide_layouts[6]) + shape = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(8), Inches(2)) + shape.text = "This paragraph has no explicit emphasis." + file = io.BytesIO() + presentation.save(file) + + element = partition_pptx(file=file, languages=["eng"])[0] + + assert element.metadata.emphasized_text_contents is None + assert element.metadata.emphasized_text_tags is None diff --git a/unstructured/partition/pptx.py b/unstructured/partition/pptx.py index cca1795f67..4ac2e96064 100644 --- a/unstructured/partition/pptx.py +++ b/unstructured/partition/pptx.py @@ -266,6 +266,26 @@ def _iter_picture_elements(self, picture: Picture) -> Iterator[Element]: PicturePartitionerCls = self._opts.picture_partitioner yield from PicturePartitionerCls.iter_elements(picture, self._opts) + def _paragraph_metadata(self, paragraph: _Paragraph, category_depth: int) -> ElementMetadata: + """Preserve explicit run and paragraph-default bold/italic formatting.""" + metadata = self._opts.text_metadata(category_depth=category_depth) + contents: list[str] = [] + tags: list[str] = [] + for run in paragraph.runs: + text = _break_lines(run.text).strip() + if not text: + continue + for attribute, tag in (("bold", "b"), ("italic", "i")): + emphasized = getattr(run.font, attribute) + if emphasized is None: + emphasized = getattr(paragraph.font, attribute) + if emphasized: + contents.append(text) + tags.append(tag) + metadata.emphasized_text_contents = contents or None + metadata.emphasized_text_tags = tags or None + return metadata + def _iter_shape_elements(self, shape: Shape) -> Iterator[Element]: """Generate Text or subtype element for each paragraph in `shape`.""" if self._shape_is_off_slide(shape): @@ -277,7 +297,7 @@ def _iter_shape_elements(self, shape: Shape) -> Iterator[Element]: continue level = paragraph.level or 0 - metadata = self._opts.text_metadata(category_depth=level) + metadata = self._paragraph_metadata(paragraph, category_depth=level) if self._is_bulleted_paragraph(paragraph): yield ListItem(text=text, metadata=metadata, detection_origin=DETECTION_ORIGIN) @@ -291,7 +311,7 @@ def _iter_shape_elements(self, shape: Shape) -> Iterator[Element]: ) elif is_possible_title(text): # If text is a title but not the title shape increment the category depth) - metadata = self._opts.text_metadata(category_depth=level + 1) + metadata.category_depth = level + 1 yield Title(text=text, metadata=metadata, detection_origin=DETECTION_ORIGIN) else: yield Text(text=text, metadata=metadata, detection_origin=DETECTION_ORIGIN) @@ -336,20 +356,20 @@ def _iter_title_shape_element(self, shape: Shape) -> Iterator[Element]: bullet_depth = paragraph.level or 0 yield ListItem( text=text, - metadata=self._opts.text_metadata(category_depth=bullet_depth), + metadata=self._paragraph_metadata(paragraph, category_depth=bullet_depth), detection_origin=DETECTION_ORIGIN, ) elif is_email_address(text): yield EmailAddress( text=text, - metadata=self._opts.text_metadata(category_depth=depth), + metadata=self._paragraph_metadata(paragraph, category_depth=depth), detection_origin=DETECTION_ORIGIN, ) else: # increment the category depth by the paragraph increment in the shape yield Title( text=text, - metadata=self._opts.text_metadata(category_depth=depth), + metadata=self._paragraph_metadata(paragraph, category_depth=depth), detection_origin=DETECTION_ORIGIN, ) depth += 1 # Cannot enumerate because we want to skip empty paragraphs