From 6c31137cf491dc818bb4a0ca24d68e04dd75e924 Mon Sep 17 00:00:00 2001 From: python Date: Thu, 11 Dec 2025 09:41:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E6=96=87=E6=A1=A3=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=92=8C=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=96=87=E4=BB=B6=E4=BF=A1=E6=81=AF=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E6=94=B9=E8=BF=9B=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=9C=BA=E5=88=B6=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=9C=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=AE=B5=E8=90=BD=E5=92=8C=E8=A1=A8=E6=A0=BC?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E7=A8=B3=E5=AE=9A=E6=80=A7=E5=92=8C=E8=AF=A6?= =?UTF-8?q?=E7=BB=86=E9=94=99=E8=AF=AF=E8=AE=B0=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 14 ++--- services/document_service.py | 101 ++++++++++++++++++++++++----------- 2 files changed, 79 insertions(+), 36 deletions(-) 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: """