fc / apps /documents /services /document_template /placeholder_extractor.py
jun2024's picture
Upload 2241 files
b4f2238 verified
Raw History Blame Contribute Delete
1.06 kB
"""Business logic services."""
import logging
import re
logger = logging.getLogger(__name__)
PLACEHOLDER_PATTERN = re.compile(r"\{\{\s*([\w\u4e00-\u9fff][\w\u4e00-\u9fff\.\(\)]*)\s*\}\}")
def extract_placeholders(file_path: str) -> list[str]:
placeholders: set[str] = set()
try:
from docxtpl import DocxTemplate
doc = DocxTemplate(file_path)
undeclared = doc.get_undeclared_template_variables()
placeholders.update(undeclared)
except Exception:
logger.exception("操作失败")
pass
try:
from docx import Document
word_doc = Document(file_path)
for para in word_doc.paragraphs:
placeholders.update(PLACEHOLDER_PATTERN.findall(para.text))
for table in word_doc.tables:
for row in table.rows:
for cell in row.cells:
placeholders.update(PLACEHOLDER_PATTERN.findall(cell.text))
except Exception:
logger.exception("操作失败")
pass
return sorted(list(placeholders))