fad458b2a7
- UIUX 文档:填充 19 个缺口(多主体画像/健康度/AI+看板/增长域/洞察域/创始人端/OODA/助推/商密) - UIUX 文档:插入 6 个新章节(十四~十九),旧章节重编号为二十~三十一,更新目录和交叉引用 - 作业指导书 x5:导航改为 6 域分组,新增 Context Bar/工作模式/Insight Rail/决策线程/多工作区等 UI 概念 - 新建 docs/2-task-uiux.md:50 个代码落地开发任务,按 P0-P6 分优先级 + 8 Sprint 规划 - 后端/前端:大量新增模型、路由、组件(来自之前 Phase 开发)
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
"""文件解析服务。
|
|
|
|
Excel/PDF 文件解析 → 文本提取。
|
|
"""
|
|
|
|
|
|
async def parse_excel(file_bytes: bytes) -> str:
|
|
"""解析 Excel 文件,提取文本内容。"""
|
|
try:
|
|
import openpyxl
|
|
import io
|
|
wb = openpyxl.load_workbook(io.BytesIO(file_bytes), read_only=True)
|
|
texts: list[str] = []
|
|
for sheet in wb.sheetnames:
|
|
ws = wb[sheet]
|
|
for row in ws.iter_rows(values_only=True):
|
|
row_text = " | ".join(str(c) for c in row if c is not None)
|
|
if row_text.strip():
|
|
texts.append(row_text)
|
|
return "\n".join(texts)
|
|
except Exception:
|
|
return ""
|
|
|
|
|
|
async def parse_pdf(file_bytes: bytes) -> str:
|
|
"""解析 PDF 文件,提取文本内容。"""
|
|
try:
|
|
import fitz
|
|
import io
|
|
doc = fitz.open(stream=io.BytesIO(file_bytes), filetype="pdf")
|
|
texts: list[str] = []
|
|
for page in doc:
|
|
texts.append(page.get_text())
|
|
return "\n".join(texts)
|
|
except Exception:
|
|
return ""
|
|
|
|
|
|
async def parse_file(file_bytes: bytes, filename: str) -> str:
|
|
"""根据文件类型选择解析器。"""
|
|
if filename.endswith((".xlsx", ".xls")):
|
|
return await parse_excel(file_bytes)
|
|
elif filename.endswith(".pdf"):
|
|
return await parse_pdf(file_bytes)
|
|
elif filename.endswith((".txt", ".md", ".csv")):
|
|
return file_bytes.decode("utf-8", errors="ignore")
|
|
return ""
|