File size: 2,241 Bytes
5301c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from typing import Dict, Any
from starfish.data_ingest.parsers.base_parser import BaseParser


class PPTParser(BaseParser):
    """Parser for PowerPoint presentations"""

    def __init__(self):
        super().__init__()
        self.supported_extensions = [".pptx"]
        self.metadata = {}

    def parse(self, file_path: str) -> str:
        """Parse a PPTX file into plain text

        Args:
            file_path: Path to the PPTX file

        Returns:
            Extracted text from the presentation
        """
        try:
            from pptx import Presentation
        except ImportError:
            raise ImportError("python-pptx is required for PPTX parsing. Install it with: pip install python-pptx")

        prs = Presentation(file_path)

        # Extract metadata
        self.metadata = {
            "title": prs.core_properties.title,
            "author": prs.core_properties.author,
            "created": prs.core_properties.created,
            "modified": prs.core_properties.modified,
            "slides": len(prs.slides),
        }

        # Extract text from slides
        all_text = []

        for i, slide in enumerate(prs.slides):
            slide_text = []
            slide_text.append(f"--- Slide {i+1} ---")

            # Get slide title
            if slide.shapes.title and slide.shapes.title.text:
                slide_text.append(f"Title: {slide.shapes.title.text}")

            # Get text from shapes
            for shape in slide.shapes:
                if hasattr(shape, "text") and shape.text:
                    slide_text.append(shape.text)

            all_text.append("\n".join(slide_text))

        return "\n\n".join(all_text)

    def get_metadata(self) -> Dict[str, Any]:
        """Get presentation metadata

        Returns:
            Dictionary containing presentation metadata
        """
        return self.metadata

    def is_supported(self, file_path: str) -> bool:
        """Check if the file is supported by this parser

        Args:
            file_path: Path to the file

        Returns:
            True if the file is supported, False otherwise
        """
        return os.path.splitext(file_path)[1].lower() in self.supported_extensions