# /*
#  * 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 docx import Document
import time
from .base_parser import BaseParser

"""
docx模块
"""
class DOCXParser(BaseParser):

    def parser_docx(self, file_path) -> str:
        docx = Document(file_path)
        content_list = []
        null_text = str(time.time())
        # 解析普通段落
        for paragraph in docx.paragraphs:
            content_list.append(paragraph.text)

        # 遍历文档中的所有表格，并提取表格中的文本
        for table in docx.tables:
            for row in table.rows:
                row_temp = []
                for cell in row.cells:
                    if cell.text != null_text:
                        row_temp.append(cell.text)
                        cell.text = null_text
                joined_row = ','.join(row_temp)
                content_list.append(joined_row)
        result = "".join(content_list)
        return [result]

    def parser(self, file_path) -> str:
        """
        解析docx文件
        """
        return self.parser_docx(file_path)


if __name__ == '__main__':
    DocxParser = DOCXParser()
    paras = DocxParser.parser('/home/wangyan/操作系统大会演示素材v0.2/AIPC出货规模分析预测.docx')
    print(paras)
