增强文档生成和处理逻辑,兼容文件信息字段,改进错误处理机制,确保在处理段落和表格时的稳定性和详细错误记录。

This commit is contained in:
python 2025-12-11 09:41:31 +08:00
parent 4897c96b05
commit 6c31137cf4
2 changed files with 79 additions and 36 deletions

14
app.py
View File

@ -651,11 +651,12 @@ def generate_document():
first_document_name = None # 用于存储第一个生成的文档名
for file_info in file_list:
file_id = file_info.get('fileId')
file_name = file_info.get('fileName', '')
# 兼容 id 和 fileId 两种字段
file_id = file_info.get('fileId') or file_info.get('id')
file_name = file_info.get('fileName') or file_info.get('name', '')
if not file_id:
return error_response(1001, f"文件 {file_name} 缺少fileId参数")
return error_response(1001, f"文件 {file_name} 缺少fileId或id参数")
try:
# 生成文档使用fileId而不是templateCode
@ -778,11 +779,12 @@ def get_document_by_task():
first_document_name = None # 用于存储第一个生成的文档名
for file_info in file_list:
file_id = file_info.get('fileId')
file_name = file_info.get('fileName', '')
# 兼容 id 和 fileId 两种字段
file_id = file_info.get('fileId') or file_info.get('id')
file_name = file_info.get('fileName') or file_info.get('name', '')
if not file_id:
return error_response(1001, f"文件 {file_name} 缺少fileId参数")
return error_response(1001, f"文件 {file_name} 缺少fileId或id参数")
try:
# 生成文档使用fileId而不是templateCode

View File

@ -136,6 +136,7 @@ class DocumentService:
def replace_placeholder_in_paragraph(paragraph):
"""在段落中替换占位符处理跨run的情况"""
try:
# 获取段落完整文本
full_text = paragraph.text
if not full_text:
@ -151,26 +152,58 @@ class DocumentService:
has_placeholder = True
replaced_text = replaced_text.replace(placeholder, field_value or '')
# 如果有替换,更新段落文本
# 如果有替换,使用安全的方式更新段落文本
if has_placeholder:
try:
# 方法1直接设置text推荐会自动处理run
paragraph.text = replaced_text
except Exception as e1:
# 如果方法1失败尝试方法2手动处理run
try:
# 清空所有run
paragraph.clear()
# 添加新的run并设置替换后的文本
# 添加新的run
if replaced_text:
paragraph.add_run(replaced_text)
except Exception as e2:
# 如果两种方法都失败,记录错误但继续
print(f"[WARN] 无法更新段落文本方法1错误: {str(e1)}, 方法2错误: {str(e2)}")
pass
except Exception as e:
# 如果单个段落处理失败,记录错误但继续处理其他段落
print(f"[WARN] 处理段落时出错: {str(e)}")
import traceback
print(traceback.format_exc())
pass
# 替换段落中的占位符
for paragraph in doc.paragraphs:
replace_placeholder_in_paragraph(paragraph)
# 替换表格中的占位符
try:
for table in doc.tables:
if not table.rows:
continue
for row in table.rows:
if not row.cells:
continue
for cell in row.cells:
try:
# 检查cell是否有paragraphs属性且不为空
if hasattr(cell, 'paragraphs') and cell.paragraphs:
for paragraph in cell.paragraphs:
if hasattr(cell, 'paragraphs'):
# 安全地获取paragraphs列表
paragraphs = list(cell.paragraphs) if cell.paragraphs else []
for paragraph in paragraphs:
replace_placeholder_in_paragraph(paragraph)
except Exception as e:
# 如果单个单元格处理失败,记录错误但继续处理其他单元格
print(f"[WARN] 处理表格单元格时出错: {str(e)}")
pass
except Exception as e:
# 如果表格处理失败,记录错误但继续保存文档
print(f"[WARN] 处理表格时出错: {str(e)}")
pass
# 保存到临时文件
temp_dir = tempfile.gettempdir()
@ -179,8 +212,16 @@ class DocumentService:
return output_file
except IndexError as e:
# 索引越界错误,提供更详细的错误信息
import traceback
error_detail = traceback.format_exc()
raise Exception(f"填充模板失败: list index out of range. 详细信息: {str(e)}\n{error_detail}")
except Exception as e:
raise Exception(f"填充模板失败: {str(e)}")
# 其他错误,提供详细的错误信息
import traceback
error_detail = traceback.format_exc()
raise Exception(f"填充模板失败: {str(e)}\n{error_detail}")
def upload_to_minio(self, file_path: str, file_name: str) -> str:
"""