|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | + |
| 5 | +from pptx.presentation import Presentation |
| 6 | +from pptx.slide import Slide |
| 7 | + |
| 8 | +from ragbits.document_search.documents.document import DocumentMeta |
| 9 | +from ragbits.document_search.documents.element import ElementLocation, TextElement |
| 10 | + |
| 11 | + |
| 12 | +class BasePptxExtractor(ABC): |
| 13 | + """Base class for all PPTX content extractors.""" |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 17 | + """Extract content from the presentation or specific slide.""" |
| 18 | + |
| 19 | + @abstractmethod |
| 20 | + def get_extractor_name(self) -> str: |
| 21 | + """Get the name of this extractor.""" |
| 22 | + |
| 23 | + |
| 24 | +class PptxTextExtractor(BasePptxExtractor): |
| 25 | + """Extracts text content with hierarchy, positioning, and formatting.""" |
| 26 | + |
| 27 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 28 | + """Extract text content from the presentation or a specific slide.""" |
| 29 | + slides = [slide] if slide else list(presentation.slides) |
| 30 | + |
| 31 | + elements: list[TextElement] = [] |
| 32 | + for slide_idx, sld in enumerate(slides, start=1): |
| 33 | + for shape in sld.shapes: |
| 34 | + if shape.has_text_frame: |
| 35 | + text_frame = shape.text_frame |
| 36 | + text = str(text_frame.text).strip() |
| 37 | + element = TextElement( |
| 38 | + element_type="text", |
| 39 | + document_meta=document_meta, |
| 40 | + location=ElementLocation(page_number=slide_idx, coordinates={"left": shape.left, "top": shape.top, "width": shape.width, "height": shape.height}), |
| 41 | + content=text, |
| 42 | + ) |
| 43 | + elements.append(element) |
| 44 | + |
| 45 | + return elements |
| 46 | + |
| 47 | + def get_extractor_name(self) -> str: |
| 48 | + """Get the name of this extractor.""" |
| 49 | + return "pptx_text_extractor" |
| 50 | + |
| 51 | +class PptxHyperlinkExtractor(BasePptxExtractor): |
| 52 | + """Extracts text content with hierarchy, positioning, and formatting.""" |
| 53 | + |
| 54 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 55 | + """Extract hyperlink content from the presentation or a specific slide.""" |
| 56 | + slides = [slide] if slide else list(presentation.slides) |
| 57 | + |
| 58 | + elements: list[TextElement] = [] |
| 59 | + for slide_idx, sld in enumerate(slides, start=1): |
| 60 | + for shape in sld.shapes: |
| 61 | + if shape.click_action.hyperlink.address: |
| 62 | + shape.has_text_frame |
| 63 | + element = TextElement( |
| 64 | + element_type="text", |
| 65 | + document_meta=document_meta, |
| 66 | + location=ElementLocation(page_number=slide_idx, coordinates={"left": shape.left, "top": shape.top, "width": shape.width, "height": shape.height}), |
| 67 | + content=shape.click_action.hyperlink.address, |
| 68 | + ) |
| 69 | + elements.append(element) |
| 70 | + |
| 71 | + return elements |
| 72 | + |
| 73 | + |
| 74 | + def get_extractor_name(self) -> str: |
| 75 | + """Get the name of this extractor.""" |
| 76 | + return "pptx_hyperlink_extractor" |
| 77 | + |
| 78 | +class PptxImageExtractor(BasePptxExtractor): |
| 79 | + """Extracts text content with hierarchy, positioning, and formatting.""" |
| 80 | + |
| 81 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 82 | + """Extract hyperlink content from the presentation or a specific slide.""" |
| 83 | + slides = [slide] if slide else list(presentation.slides) |
| 84 | + |
| 85 | + elements: list[TextElement] = [] |
| 86 | + for slide_idx, sld in enumerate(slides, start=1): |
| 87 | + for shape in sld.shapes: |
| 88 | + if shape.click_action.hyperlink: |
| 89 | + text_frame = shape.text_frame |
| 90 | + text = str(text_frame.text).strip() |
| 91 | + element = TextElement( |
| 92 | + element_type="text", |
| 93 | + document_meta=document_meta, |
| 94 | + location=ElementLocation(page_number=slide_idx, coordinates={"left": shape.left, "top": shape.top, "width": shape.width, "height": shape.height}), |
| 95 | + content=text, |
| 96 | + ) |
| 97 | + elements.append(element) |
| 98 | + |
| 99 | + return elements |
| 100 | + |
| 101 | + |
| 102 | + def get_extractor_name(self) -> str: |
| 103 | + """Get the name of this extractor.""" |
| 104 | + return "pptx_image_extractor" |
| 105 | + |
| 106 | + |
| 107 | +class PptxShapeExtractor(BasePptxExtractor): |
| 108 | + """Extracts text content with hierarchy, positioning, and formatting.""" |
| 109 | + |
| 110 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 111 | + """Extract hyperlink content from the presentation or a specific slide.""" |
| 112 | + slides = [slide] if slide else list(presentation.slides) |
| 113 | + |
| 114 | + elements: list[TextElement] = [] |
| 115 | + for slide_idx, sld in enumerate(slides, start=1): |
| 116 | + for shape in sld.shapes: |
| 117 | + if shape.click_action.hyperlink: |
| 118 | + text_frame = shape.text_frame |
| 119 | + text = str(text_frame.text).strip() |
| 120 | + element = TextElement( |
| 121 | + element_type="text", |
| 122 | + document_meta=document_meta, |
| 123 | + location=ElementLocation(page_number=slide_idx, coordinates={"left": shape.left, "top": shape.top, "width": shape.width, "height": shape.height}), |
| 124 | + content=text, |
| 125 | + ) |
| 126 | + elements.append(element) |
| 127 | + |
| 128 | + return elements |
| 129 | + |
| 130 | + |
| 131 | + def get_extractor_name(self) -> str: |
| 132 | + """Get the name of this extractor.""" |
| 133 | + return "pptx_shape_extractor" |
| 134 | + |
| 135 | +class PptxMetadataExtractor(BasePptxExtractor): |
| 136 | + """Extracts text content with hierarchy, positioning, and formatting.""" |
| 137 | + |
| 138 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 139 | + """Extract hyperlink content from the presentation or a specific slide.""" |
| 140 | + core_properties = presentation.core_properties |
| 141 | + properties = [ |
| 142 | + core_properties.author, |
| 143 | + core_properties.title, |
| 144 | + core_properties.subject, |
| 145 | + core_properties.keywords, |
| 146 | + core_properties.category, |
| 147 | + core_properties.created, |
| 148 | + core_properties.modified, |
| 149 | + ] |
| 150 | + |
| 151 | + elements = [] |
| 152 | + for prop in properties: |
| 153 | + if prop is not None: |
| 154 | + elements.append(TextElement( |
| 155 | + element_type="metadata", |
| 156 | + document_meta=document_meta, |
| 157 | + content=prop, |
| 158 | + )) |
| 159 | + |
| 160 | + return elements |
| 161 | + |
| 162 | + |
| 163 | + def get_extractor_name(self) -> str: |
| 164 | + """Get the name of this extractor.""" |
| 165 | + return "pptx_metadata_extractor" |
| 166 | + |
| 167 | +class PptxSpeakerNotesExtractor(BasePptxExtractor): |
| 168 | + """Extracts text content with hierarchy, positioning, and formatting.""" |
| 169 | + |
| 170 | + def extract(self, presentation: Presentation, document_meta: DocumentMeta, slide: Slide | None = None) -> list[TextElement]: |
| 171 | + """Extract hyperlink content from the presentation or a specific slide.""" |
| 172 | + slides = [slide] if slide else list(presentation.slides) |
| 173 | + |
| 174 | + elements: list[TextElement] = [] |
| 175 | + for slide_idx, sld in enumerate(slides, start=1): |
| 176 | + if sld.has_notes_slide and sld.notes_slide.notes_text_frame is not None: |
| 177 | + notes_slide = sld.notes_slide |
| 178 | + notes_text_frame = notes_slide.notes_text_frame |
| 179 | + text = notes_text_frame.text |
| 180 | + element = TextElement( |
| 181 | + element_type="text", |
| 182 | + document_meta=document_meta, |
| 183 | + location=ElementLocation(page_number=slide_idx, coordinates={"left": notes_text_frame.margin_left, "right": notes_text_frame.margin_right, "top": notes_text_frame.margin_top, "bottom": notes_text_frame.margin_bottom}), |
| 184 | + content=text, |
| 185 | + ) |
| 186 | + elements.append(element) |
| 187 | + for shape in notes_slide.shapes: |
| 188 | + if shape.has_text_frame: |
| 189 | + text_frame = shape.text_frame |
| 190 | + text = str(text_frame.text).strip() |
| 191 | + element = TextElement( |
| 192 | + element_type="text", |
| 193 | + document_meta=document_meta, |
| 194 | + location=ElementLocation(page_number=slide_idx, coordinates={"left": shape.left, "top": shape.top, "width": shape.width, "height": shape.height}), |
| 195 | + content=text, |
| 196 | + ) |
| 197 | + elements.append(element) |
| 198 | + |
| 199 | + return elements |
| 200 | + |
| 201 | + |
| 202 | + def get_extractor_name(self) -> str: |
| 203 | + """Get the name of this extractor.""" |
| 204 | + return "pptx_speaker_notes_extractor" |
| 205 | + |
| 206 | +DEFAULT_EXTRACTORS = [ |
| 207 | + PptxTextExtractor(), |
| 208 | + PptxHyperlinkExtractor(), |
| 209 | + PptxImageExtractor(), |
| 210 | + PptxShapeExtractor(), |
| 211 | + PptxSpeakerNotesExtractor(), |
| 212 | + PptxMetadataExtractor(), |
| 213 | + # PptxSlideImageExtractor(), |
| 214 | +] |
0 commit comments