#!/usr/bin/env python3 """ 测试实际的报告生成流程,模拟真实环境 """ import json import sys import os import time from pathlib import Path def test_with_real_data(): """使用真实数据测试报告生成""" print("使用真实数据测试报告生成") print("=" * 50) # 你的实际JSON数据(简化版) real_json_data = { "tables": [{ "token": "scriptTable1", "startRow": 0, "startCol": 0, "cells": [ {"row": 4, "col": 0, "value": "23.2"}, {"row": 4, "col": 1, "value": "27.5"}, {"row": 4, "col": 2, "value": "27.6"}, {"row": 5, "col": 0, "value": "24.2"}, {"row": 5, "col": 1, "value": "27.7"}, {"row": 6, "col": 0, "value": "24.9"}, {"row": 20, "col": 6, "value": "28.5"}, {"row": 1, "col": 1, "value": "2025-11-27 11:30:00"}, {"row": 1, "col": 3, "value": "2025-11-27 15:00:00"}, {"row": 0, "col": 1, "value": "16.5"} ] }] } try: import win32com.client from win32com.client import constants print("创建Word应用程序...") word_app = win32com.client.Dispatch("Word.Application") word_app.Visible = True # 显示Word窗口以便观察 print("创建新文档...") doc = word_app.Documents.Add() print("插入表格 (25行 x 10列)...") table = doc.Tables.Add(doc.Range(0, 0), 25, 10) # 在第一个单元格添加scriptTable1标记 table.Cell(1, 1).Range.Text = "scriptTable1" print("✓ 表格创建完成,已添加token标记") # 模拟report_generator.py的处理逻辑 table_spec = real_json_data["tables"][0] cells = table_spec["cells"] print(f"开始处理 {len(cells)} 个单元格...") processed = 0 errors = 0 start_time = time.time() for i, cell_info in enumerate(cells): try: row = cell_info["row"] + 1 # Word索引从1开始 col = cell_info["col"] + 1 value = str(cell_info["value"]) # 检查范围 if row > 25 or col > 10: print(f" 跳过超出范围的单元格: ({row}, {col})") continue # 写入单元格 cell_obj = table.Cell(row, col) cell_obj.Range.Text = value processed += 1 # 进度报告 if (i + 1) % 5 == 0: elapsed = time.time() - start_time print(f" 进度: {i + 1}/{len(cells)} ({elapsed:.1f}秒)") # 检查是否卡住(超过30秒) if elapsed > 30: print("⚠️ 处理时间过长,可能卡住了") break except Exception as e: print(f" ✗ 处理单元格 ({row}, {col}) 失败: {e}") errors += 1 if errors > 3: print("错误过多,停止处理") break total_time = time.time() - start_time print(f"\n处理完成:") print(f" 成功: {processed} 个单元格") print(f" 错误: {errors} 个单元格") print(f" 总耗时: {total_time:.1f} 秒") if total_time > 10: print("⚠️ 处理时间较长,可能存在性能问题") else: print("✓ 处理时间正常") # 保存测试文档 output_path = Path("E:/docx_creatorV2/test_report_output.docx") try: doc.SaveAs(str(output_path.absolute())) print(f"✓ 测试文档已保存: {output_path}") except Exception as e: print(f"保存文档失败: {e}") print("\n请检查Word窗口中的结果,然后按Enter继续...") input() doc.Close(SaveChanges=False) word_app.Quit() return processed > 0 and errors == 0 except Exception as e: print(f"✗ 测试失败: {e}") try: word_app.Quit() except: pass return False def test_template_with_scriptTable1(): """测试在实际模板中查找scriptTable1""" print("\n测试模板中的scriptTable1标记") print("=" * 50) # 查找模板文件 template_paths = [ Path("E:/docx_creatorV2/template-001.docx"), Path("E:/docx_creatorV2/template-20250926.docx"), Path("E:/docx_creatorV2/1000泵跑合.docx"), Path("template-001.docx"), Path("template-20250926.docx") ] template_file = None for path in template_paths: if path.exists(): template_file = path break if not template_file: print("⚠️ 未找到模板文件") return False print(f"使用模板: {template_file}") try: import win32com.client word_app = win32com.client.Dispatch("Word.Application") word_app.Visible = True doc = word_app.Documents.Open(str(template_file.absolute())) print("✓ 模板打开成功") # 查找scriptTable1标记 found_token = False token_location = None if doc.Tables.Count > 0: print(f"模板中有 {doc.Tables.Count} 个表格") for table_idx in range(1, doc.Tables.Count + 1): table = doc.Tables(table_idx) print(f"检查表格 {table_idx}: {table.Rows.Count} 行 x {table.Columns.Count} 列") # 搜索前几行几列 for row in range(1, min(table.Rows.Count + 1, 6)): for col in range(1, min(table.Columns.Count + 1, 6)): try: cell_text = table.Cell(row, col).Range.Text.strip() if "scriptTable1" in cell_text: print(f"✓ 找到scriptTable1在表格{table_idx}的({row}, {col})") found_token = True token_location = (table_idx, row, col) break except: continue if found_token: break if found_token: break if not found_token: print("⚠️ 未找到scriptTable1标记") print("建议:") print("1. 检查模板文件是否正确") print("2. 确认scriptTable1标记的位置") print("3. 可能需要手动在模板中添加标记") print("\n请检查模板内容,然后按Enter继续...") input() doc.Close(SaveChanges=False) word_app.Quit() return found_token except Exception as e: print(f"✗ 模板测试失败: {e}") try: word_app.Quit() except: pass return False def main(): print("实际报告生成测试") print("=" * 60) # 测试1: 使用真实数据 data_test_ok = test_with_real_data() # 测试2: 检查模板 template_test_ok = test_template_with_scriptTable1() print("\n" + "=" * 60) print("测试结果:") print(f"真实数据处理: {'✓ 通过' if data_test_ok else '✗ 失败'}") print(f"模板检查: {'✓ 通过' if template_test_ok else '✗ 失败'}") if data_test_ok and not template_test_ok: print("\n分析:") print("- Word COM操作正常") print("- 可能是模板文件中缺少scriptTable1标记") print("- 建议检查模板文件或手动添加标记") elif not data_test_ok: print("\n分析:") print("- Word COM操作可能有问题") print("- 建议检查系统资源和Word版本") if __name__ == "__main__": main()