2024-03-29 10:28:05 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: maxkb
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: text_split_handle.py
|
|
|
|
|
|
@date:2024/3/27 18:19
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
import io
|
|
|
|
|
|
import re
|
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
from docx import Document
|
2024-04-10 06:16:56 +00:00
|
|
|
|
from docx.table import Table
|
|
|
|
|
|
from docx.text.paragraph import Paragraph
|
2024-03-29 10:28:05 +00:00
|
|
|
|
|
|
|
|
|
|
from common.handle.base_split_handle import BaseSplitHandle
|
|
|
|
|
|
from common.util.split_model import SplitModel
|
|
|
|
|
|
|
2024-04-10 06:16:56 +00:00
|
|
|
|
default_pattern_list = [re.compile('(?<=^)# .*|(?<=\\n)# .*'),
|
|
|
|
|
|
re.compile('(?<=\\n)(?<!#)## (?!#).*|(?<=^)(?<!#)## (?!#).*'),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)### (?!#).*|(?<=^)(?<!#)### (?!#).*"),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)#### (?!#).*|(?<=^)(?<!#)#### (?!#).*"),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)##### (?!#).*|(?<=^)(?<!#)##### (?!#).*"),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)###### (?!#).*|(?<=^)(?<!#)###### (?!#).*")]
|
2024-03-29 10:28:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DocSplitHandle(BaseSplitHandle):
|
2024-04-01 06:39:56 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def paragraph_to_md(paragraph):
|
2024-04-09 07:56:01 +00:00
|
|
|
|
try:
|
|
|
|
|
|
psn = paragraph.style.name
|
|
|
|
|
|
if psn.startswith('Heading'):
|
2024-04-01 06:39:56 +00:00
|
|
|
|
return "".join(["#" for i in range(int(psn.replace("Heading ", '')))]) + " " + paragraph.text
|
2024-04-09 07:56:01 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
return paragraph.text
|
2024-04-01 06:39:56 +00:00
|
|
|
|
return paragraph.text
|
|
|
|
|
|
|
2024-04-10 06:16:56 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def table_to_md(table):
|
|
|
|
|
|
rows = table.rows
|
|
|
|
|
|
# 创建 Markdown 格式的表格
|
|
|
|
|
|
md_table = '| ' + ' | '.join([cell.text.replace("\n", '</br>') for cell in rows[0].cells]) + ' |\n'
|
|
|
|
|
|
md_table += '| ' + ' | '.join(['---' for i in range(len(rows[0].cells))]) + ' |\n'
|
|
|
|
|
|
for row in rows[1:]:
|
|
|
|
|
|
md_table += '| ' + ' | '.join([cell.text.replace("\n", '</br>') for cell in row.cells]) + ' |\n'
|
|
|
|
|
|
return md_table
|
|
|
|
|
|
|
2024-04-01 06:39:56 +00:00
|
|
|
|
def to_md(self, doc):
|
2024-04-10 06:16:56 +00:00
|
|
|
|
elements = []
|
|
|
|
|
|
for element in doc.element.body:
|
|
|
|
|
|
if element.tag.endswith('tbl'):
|
|
|
|
|
|
# 处理表格
|
|
|
|
|
|
table = Table(element, doc)
|
|
|
|
|
|
elements.append(table)
|
|
|
|
|
|
elif element.tag.endswith('p'):
|
|
|
|
|
|
# 处理段落
|
|
|
|
|
|
paragraph = Paragraph(element, doc)
|
|
|
|
|
|
elements.append(paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
return "\n".join(
|
|
|
|
|
|
[self.paragraph_to_md(element) if isinstance(element, Paragraph) else self.table_to_md(element) for element
|
|
|
|
|
|
in elements])
|
2024-04-01 06:39:56 +00:00
|
|
|
|
|
2024-03-29 10:28:05 +00:00
|
|
|
|
def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_buffer):
|
|
|
|
|
|
try:
|
|
|
|
|
|
buffer = get_buffer(file)
|
|
|
|
|
|
doc = Document(io.BytesIO(buffer))
|
2024-04-01 06:39:56 +00:00
|
|
|
|
content = self.to_md(doc)
|
2024-03-29 10:28:05 +00:00
|
|
|
|
if pattern_list is not None and len(pattern_list) > 0:
|
|
|
|
|
|
split_model = SplitModel(pattern_list, with_filter, limit)
|
|
|
|
|
|
else:
|
|
|
|
|
|
split_model = SplitModel(default_pattern_list, with_filter=with_filter, limit=limit)
|
|
|
|
|
|
except BaseException as e:
|
|
|
|
|
|
return {'name': file.name,
|
|
|
|
|
|
'content': []}
|
|
|
|
|
|
return {'name': file.name,
|
|
|
|
|
|
'content': split_model.parse(content)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def support(self, file, get_buffer):
|
|
|
|
|
|
file_name: str = file.name.lower()
|
|
|
|
|
|
if file_name.endswith(".docx") or file_name.endswith(".doc"):
|
|
|
|
|
|
return True
|
|
|
|
|
|
return False
|