فهرست منبع

modify:支持插入多个表格

zhusc 1 روز پیش
والد
کامیت
805d6c1bce
1فایلهای تغییر یافته به همراه67 افزوده شده و 14 حذف شده
  1. 67 14
      commom/utils.py

+ 67 - 14
commom/utils.py

@@ -169,21 +169,74 @@ def f_doc_export(token: str, request_id: str, data: object) -> str:
         f.write(response3.file.read())
     time.sleep(2)
 
-    # 操作word
-    if data is not None:
-        doc = Document(save_path)
-        placeholder = "{TABLE_PLACEHOLDER}"
+    # # 操作word
+    # if data is not None:
+    #     doc = Document(save_path)
+    #     placeholder = "{TABLE_PLACEHOLDER}"
+    #     for paragraph in doc.paragraphs:
+    #         if not placeholder in paragraph.text:
+    #             continue
+    #         # 清除占位符
+    #         for run in paragraph.runs:
+    #             run.text = run.text.replace(placeholder, "")
+    #         # 生成表格(调用改造后的 create_word_table 函数,传入字符串)
+    #         table = create_word_table(data)
+    #         paragraph._element.addnext(table._tbl)
+    #     doc.save(save_path)
+    #     time.sleep(2)
+    #
+    # doc = Document(save_path)
+    # placeholder_base = "{TABLE_PLACEHOLDER}"  # 基础占位符前缀
+
+
+    # # table_datas = data
+    #
+    # # 遍历所有段落,按索引匹配占位符
+    # for idx, paragraph in enumerate(doc.paragraphs):
+    #     # 构造当前占位符(如{TABLE_PLACEHOLDER}_1, _2, _3...)
+    #     current_placeholder = f"{placeholder_base}_{idx + 1}"
+    #
+    #     if current_placeholder in paragraph.text:
+    #         # 检查是否有对应索引的表格数据
+    #         if idx < len(data):
+    #             table_json = data[idx]  # 获取第idx+1个表格数据
+    #             # 生成表格(假设create_word_table接收JSON字符串或字典)
+    #             if isinstance(table_json, str):
+    #                 table = create_word_table(table_json)  # 传入JSON字符串
+    #             else:
+    #                 table = create_word_table(json.dumps(table_json))  # 传入字典需转为字符串
+    #
+    #             # 清除占位符文本
+    #             for run in paragraph.runs:
+    #                 run.text = run.text.replace(current_placeholder, "")
+    #
+    #             # 插入表格到占位符位置
+    #             paragraph._element.addnext(table._tbl)
+    #         else:
+    #             print(f"警告:占位符{current_placeholder}无对应表格数据")
+    #
+    # doc.save(save_path)
+    # time.sleep(2)
+
+    doc = Document(save_path)
+    placeholder_prefix = "{TABLE_PLACEHOLDER}_"
+    placeholder_count = len(data)
+
+    for i in range(1, placeholder_count + 1):
+        placeholder = f"{placeholder_prefix}{i}"
         for paragraph in doc.paragraphs:
-            if not placeholder in paragraph.text:
-                continue
-            # 清除占位符
-            for run in paragraph.runs:
-                run.text = run.text.replace(placeholder, "")
-            # 生成表格(调用改造后的 create_word_table 函数,传入字符串)
-            table = create_word_table(data)
-            paragraph._element.addnext(table._tbl)
-        doc.save(save_path)
-        time.sleep(2)
+            if placeholder in paragraph.text:
+                # 清除占位符
+                for run in paragraph.runs:
+                    run.text = run.text.replace(placeholder, "")
+                # 生成表格(调用改造后的 create_word_table 函数,传入字符串)
+                table_data = data[i-1]
+                table = create_word_table(table_data)
+                paragraph._element.addnext(table._tbl)
+                break  # 找到并处理一个占位符后,跳出内层循环
+
+    doc.save(save_path)
+    time.sleep(2)
 
     word_download_url = f_upload_file(save_path)