diff --git a/app.py b/app.py index 360c111..3557842 100644 --- a/app.py +++ b/app.py @@ -701,6 +701,7 @@ def generate_document(): return error_response(3001, f"文档生成失败: {str(e)}") +<<<<<<< HEAD @app.route('/fPolicTask/getDocument', methods=['POST']) def get_document_by_task(): """ @@ -829,6 +830,8 @@ def get_document_by_task(): return error_response(3001, f"文档生成失败: {str(e)}") +======= +>>>>>>> parent of 4897c96 (添加通过taskId获取文档的接口,支持文件列表查询和参数验证,增强错误处理能力。同时,优化文档生成逻辑,确保生成的文档名称和路径的准确性。) if __name__ == '__main__': # 确保static目录存在 os.makedirs('static', exist_ok=True) diff --git a/services/document_service.py b/services/document_service.py index 7bfdaab..0a00699 100644 --- a/services/document_service.py +++ b/services/document_service.py @@ -138,6 +138,7 @@ class DocumentService: doc = Document(template_path) print(f"[DEBUG] 文档包含 {len(doc.paragraphs)} 个段落, {len(doc.tables)} 个表格") +<<<<<<< HEAD def replace_placeholder_in_paragraph(paragraph): """在段落中替换占位符(处理跨run的情况)""" try: @@ -203,6 +204,18 @@ class DocumentService: if placeholder in before_text and placeholder not in after_text: replaced_placeholders.add(field_code) total_replacements += before_text.count(placeholder) +======= + # 替换占位符 {{field_code}} 为实际值 + for paragraph in doc.paragraphs: + # 替换段落文本中的占位符 + for field_code, field_value in field_data.items(): + placeholder = f"{{{{{field_code}}}}}" + if placeholder in paragraph.text: + # 替换占位符 + for run in paragraph.runs: + if placeholder in run.text: + run.text = run.text.replace(placeholder, field_value or '') +>>>>>>> parent of 4897c96 (添加通过taskId获取文档的接口,支持文件列表查询和参数验证,增强错误处理能力。同时,优化文档生成逻辑,确保生成的文档名称和路径的准确性。) # 替换表格中的占位符 try: @@ -251,6 +264,7 @@ class DocumentService: for table in doc.tables: for row in table.rows: for cell in row.cells: +<<<<<<< HEAD if hasattr(cell, 'paragraphs'): for paragraph in cell.paragraphs: text = paragraph.text @@ -267,6 +281,15 @@ class DocumentService: print(f" - ⚠️ 仍有未替换的占位符: {sorted(remaining_placeholders)}") else: print(f" - ✓ 所有占位符已成功替换") +======= + for paragraph in cell.paragraphs: + for field_code, field_value in field_data.items(): + placeholder = f"{{{{{field_code}}}}}" + if placeholder in paragraph.text: + for run in paragraph.runs: + if placeholder in run.text: + run.text = run.text.replace(placeholder, field_value or '') +>>>>>>> parent of 4897c96 (添加通过taskId获取文档的接口,支持文件列表查询和参数验证,增强错误处理能力。同时,优化文档生成逻辑,确保生成的文档名称和路径的准确性。) # 保存到临时文件 temp_dir = tempfile.gettempdir()