diff --git a/app.py b/app.py index 6e4169d..360c111 100644 --- a/app.py +++ b/app.py @@ -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) diff --git a/services/document_service.py b/services/document_service.py index 239340c..9881927 100644 --- a/services/document_service.py +++ b/services/document_service.py @@ -136,41 +136,74 @@ class DocumentService: def replace_placeholder_in_paragraph(paragraph): """在段落中替换占位符(处理跨run的情况)""" - # 获取段落完整文本 - full_text = paragraph.text - if not full_text: - return - - # 检查是否有占位符需要替换 - has_placeholder = False - replaced_text = full_text - - for field_code, field_value in field_data.items(): - placeholder = f"{{{{{field_code}}}}}" - if placeholder in replaced_text: - has_placeholder = True - replaced_text = replaced_text.replace(placeholder, field_value or '') - - # 如果有替换,更新段落文本 - if has_placeholder: - # 清空所有run - paragraph.clear() - # 添加新的run并设置替换后的文本 - if replaced_text: - paragraph.add_run(replaced_text) + try: + # 获取段落完整文本 + full_text = paragraph.text + if not full_text: + return + + # 检查是否有占位符需要替换 + has_placeholder = False + replaced_text = full_text + + for field_code, field_value in field_data.items(): + placeholder = f"{{{{{field_code}}}}}" + if placeholder in replaced_text: + 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 + 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) # 替换表格中的占位符 - for table in doc.tables: - for row in table.rows: - for cell in row.cells: - # 检查cell是否有paragraphs属性且不为空 - if hasattr(cell, 'paragraphs') and cell.paragraphs: - for paragraph in cell.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'): + # 安全地获取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: """