# /*
#  * Copyright 2024 KylinSoft Co., Ltd.
#  *
#  * This program is free software: you can redistribute it and/or modify it under
#  * the terms of the GNU General Public License as published by the Free Software
#  * Foundation, either version 3 of the License, or (at your option) any later
#  * version.
#  *
#  * This program is distributed in the hope that it will be useful, but WITHOUT
#  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  * details.
#  *
#  * You should have received a copy of the GNU General Public License along with
#  * this program. If not, see <https://www.gnu.org/licenses/>.
#  */

from pptx import Presentation
from .base_parser import BaseParser

class PPTXParser(BaseParser):

    def parser(self, file_path) -> dict:
        return self.parser_pptx(file_path)

    def parser_pptx(self, file_path) -> str:
        """
        解析PPTX文件，返回文本内容。
        :param file_path: PPTX文件路径
        :return: 文本内容
        """
        prs = Presentation(file_path)
        content = []

        for slide in prs.slides:
            slide_content = self._parse_slide(slide)
            if slide_content == ['']:
                continue
            else:
                content.extend(slide_content)
        texts = ",".join(content)
        return texts

    def _parse_slide(self, slide):
        """
        解析单个幻灯片的文本和表格。
        :param slide: PPTX 幻灯片对象
        :return: 包含文本和表格数据的元组
        """
        # 提取文本
        text = ""
        for shape in slide.shapes:
            if not shape.has_text_frame:
                continue
            for paragraph in shape.text_frame.paragraphs:
                for run in paragraph.runs:
                    text += run.text + " "

        # 提取表格
        tables = []
        for shape in slide.shapes:
            if not shape.has_table:
                continue
            for row in shape.table.rows:
                row_data = [cell.text for cell in row.cells]
                tables.append(", ".join(row_data))

        result = "".join([text.strip()] + tables)
        return [result]


if __name__ == '__main__':
    parser = PPTXParser()
    print(parser.parser('/home/wangyan/操作系统大会演示素材v0.2/团建策划资料/户外团建活动游戏(1).pptx'))
