Strip legacy control chars (0x0b etc.) at extraction and in docx report - Word XML rejects them
This commit is contained in:
1 parent
5a5cc41eba
commit
6abe5c66b4
1 file changed
+17
-7
@@ -49,6 +49,16 @@ app = FastAPI(title="docchecker", docs_url=None, redoc_url=None)
|
||||
JOBS: dict = {}
|
||||
JOBS_LOCK = threading.Lock()
|
||||
|
||||
_CTRL_RE = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]")
|
||||
|
||||
|
||||
def clean_text(s: str) -> str:
|
||||
"""Убрать управляющие символы (lxml/python-docx их не принимают; легаси Word их сыплет)."""
|
||||
if not s:
|
||||
return s
|
||||
return _CTRL_RE.sub(" ", s).replace("\r", " ")
|
||||
|
||||
|
||||
# --- разбор документа ---
|
||||
|
||||
MIN_CELL_CHARS = 20 # короче — это служебные ячейки, не проверяем
|
||||
@@ -395,7 +405,7 @@ def run_job(job_id: str, data: bytes, filename: str, use_llm: bool):
|
||||
try:
|
||||
JOBS[job_id]["stage"] = "разбор документа"
|
||||
JOBS[job_id]["progress"] = 0.02
|
||||
paras = extract_paragraphs(data, filename)
|
||||
paras = [clean_text(t) for t in extract_paragraphs(data, filename)]
|
||||
if not paras:
|
||||
raise ValueError("В документе не найдено текста")
|
||||
JOBS[job_id]["total_paras"] = len(paras)
|
||||
@@ -534,21 +544,21 @@ def api_report_docx(job_id: str):
|
||||
for f in rep["findings"]:
|
||||
row = table.add_row().cells
|
||||
row[0].text = str(f["para"])
|
||||
row[1].text = f.get("kind", "")
|
||||
row[2].paragraphs[0].add_run(f.get("fragment", "")).bold = True
|
||||
row[1].text = clean_text(f.get("kind", ""))
|
||||
row[2].paragraphs[0].add_run(clean_text(f.get("fragment", ""))).bold = True
|
||||
pc = row[3].paragraphs[0]
|
||||
b, frag, a = (f.get("context") or ["", "", ""])
|
||||
b = b.replace("\n", " ").strip()
|
||||
a = a.replace("\n", " ").strip()
|
||||
b = clean_text(b).strip()
|
||||
a = clean_text(a).strip()
|
||||
if b:
|
||||
pc.add_run("…" + b[-45:] if len(b) > 45 else b)
|
||||
if frag:
|
||||
pc.add_run(frag).bold = True
|
||||
pc.add_run(clean_text(frag)).bold = True
|
||||
else:
|
||||
pc.add_run("<—>")
|
||||
if a:
|
||||
pc.add_run(a[:45] + ("…" if len(a) > 45 else ""))
|
||||
row[4].text = " / ".join((f.get("replacements") or [])[:3]) or "—"
|
||||
row[4].text = clean_text(" / ".join((f.get("replacements") or [])[:3])) or "—"
|
||||
buf = io.BytesIO()
|
||||
doc.save(buf)
|
||||
return Response(
|
||||
|
||||
Reference in new issue
Block a user