Browse Source

bugfix: 1

yq 1 day ago
parent
commit
764160c905
2 changed files with 21 additions and 69 deletions
  1. 20 68
      commom/utils.py
  2. 1 1
      main.py

+ 20 - 68
commom/utils.py

@@ -169,74 +169,26 @@ 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}"
-    #     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 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)
+    if data is not None:
+        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 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)
 

+ 1 - 1
main.py

@@ -24,7 +24,7 @@ app = FastAPI()
 def doc_export(
         token: str = Path(title='doc token', description="文档的token"),
         request_id: str = Path(title='request id', description="请求ID作为文档名后缀"),
-        data: object|None = Body(..., embed=True, description="额外参数")
+        data: object|None = Body(None, description="额外参数")
 
 ):
     try: