增强文档生成和处理逻辑,兼容文件信息字段,改进错误处理机制,确保在处理段落和表格时的稳定性和详细错误记录。
This commit is contained in:
parent
4897c96b05
commit
6c31137cf4
14
app.py
14
app.py
@ -651,11 +651,12 @@ def generate_document():
|
|||||||
first_document_name = None # 用于存储第一个生成的文档名
|
first_document_name = None # 用于存储第一个生成的文档名
|
||||||
|
|
||||||
for file_info in file_list:
|
for file_info in file_list:
|
||||||
file_id = file_info.get('fileId')
|
# 兼容 id 和 fileId 两种字段
|
||||||
file_name = file_info.get('fileName', '')
|
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:
|
if not file_id:
|
||||||
return error_response(1001, f"文件 {file_name} 缺少fileId参数")
|
return error_response(1001, f"文件 {file_name} 缺少fileId或id参数")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 生成文档(使用fileId而不是templateCode)
|
# 生成文档(使用fileId而不是templateCode)
|
||||||
@ -778,11 +779,12 @@ def get_document_by_task():
|
|||||||
first_document_name = None # 用于存储第一个生成的文档名
|
first_document_name = None # 用于存储第一个生成的文档名
|
||||||
|
|
||||||
for file_info in file_list:
|
for file_info in file_list:
|
||||||
file_id = file_info.get('fileId')
|
# 兼容 id 和 fileId 两种字段
|
||||||
file_name = file_info.get('fileName', '')
|
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:
|
if not file_id:
|
||||||
return error_response(1001, f"文件 {file_name} 缺少fileId参数")
|
return error_response(1001, f"文件 {file_name} 缺少fileId或id参数")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 生成文档(使用fileId而不是templateCode)
|
# 生成文档(使用fileId而不是templateCode)
|
||||||
|
|||||||
@ -136,6 +136,7 @@ class DocumentService:
|
|||||||
|
|
||||||
def replace_placeholder_in_paragraph(paragraph):
|
def replace_placeholder_in_paragraph(paragraph):
|
||||||
"""在段落中替换占位符(处理跨run的情况)"""
|
"""在段落中替换占位符(处理跨run的情况)"""
|
||||||
|
try:
|
||||||
# 获取段落完整文本
|
# 获取段落完整文本
|
||||||
full_text = paragraph.text
|
full_text = paragraph.text
|
||||||
if not full_text:
|
if not full_text:
|
||||||
@ -151,26 +152,58 @@ class DocumentService:
|
|||||||
has_placeholder = True
|
has_placeholder = True
|
||||||
replaced_text = replaced_text.replace(placeholder, field_value or '')
|
replaced_text = replaced_text.replace(placeholder, field_value or '')
|
||||||
|
|
||||||
# 如果有替换,更新段落文本
|
# 如果有替换,使用安全的方式更新段落文本
|
||||||
if has_placeholder:
|
if has_placeholder:
|
||||||
|
try:
|
||||||
|
# 方法1:直接设置text(推荐,会自动处理run)
|
||||||
|
paragraph.text = replaced_text
|
||||||
|
except Exception as e1:
|
||||||
|
# 如果方法1失败,尝试方法2:手动处理run
|
||||||
|
try:
|
||||||
# 清空所有run
|
# 清空所有run
|
||||||
paragraph.clear()
|
paragraph.clear()
|
||||||
# 添加新的run并设置替换后的文本
|
# 添加新的run
|
||||||
if replaced_text:
|
if replaced_text:
|
||||||
paragraph.add_run(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:
|
for paragraph in doc.paragraphs:
|
||||||
replace_placeholder_in_paragraph(paragraph)
|
replace_placeholder_in_paragraph(paragraph)
|
||||||
|
|
||||||
# 替换表格中的占位符
|
# 替换表格中的占位符
|
||||||
|
try:
|
||||||
for table in doc.tables:
|
for table in doc.tables:
|
||||||
|
if not table.rows:
|
||||||
|
continue
|
||||||
for row in table.rows:
|
for row in table.rows:
|
||||||
|
if not row.cells:
|
||||||
|
continue
|
||||||
for cell in row.cells:
|
for cell in row.cells:
|
||||||
|
try:
|
||||||
# 检查cell是否有paragraphs属性且不为空
|
# 检查cell是否有paragraphs属性且不为空
|
||||||
if hasattr(cell, 'paragraphs') and cell.paragraphs:
|
if hasattr(cell, 'paragraphs'):
|
||||||
for paragraph in cell.paragraphs:
|
# 安全地获取paragraphs列表
|
||||||
|
paragraphs = list(cell.paragraphs) if cell.paragraphs else []
|
||||||
|
for paragraph in paragraphs:
|
||||||
replace_placeholder_in_paragraph(paragraph)
|
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()
|
temp_dir = tempfile.gettempdir()
|
||||||
@ -179,8 +212,16 @@ class DocumentService:
|
|||||||
|
|
||||||
return output_file
|
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:
|
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:
|
def upload_to_minio(self, file_path: str, file_name: str) -> str:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user