audit-e2w 1.11.2 → 1.11.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +152 -148
  2. package/dsh_plugins/audit-e2w/lib/index.js +9 -1
  3. package/dsh_plugins/audit-e2w/lib/skill.js +3 -2
  4. package/excel_word/README.md +45 -37
  5. package/excel_word/qoder_engine.py +10 -11
  6. package/excel_word/wesync/__init__.py +1 -1
  7. package/excel_word/wesync/ai/__init__.py +52 -0
  8. package/excel_word/wesync/ai/actions.py +369 -0
  9. package/excel_word/wesync/ai/client.py +340 -0
  10. package/excel_word/wesync/ai/config.py +291 -0
  11. package/excel_word/wesync/ai/docs.py +177 -0
  12. package/excel_word/wesync/ai/prompts.py +288 -0
  13. package/excel_word/wesync/ai/tasks/__init__.py +26 -0
  14. package/excel_word/wesync/ai/tasks/actions_task.py +97 -0
  15. package/excel_word/wesync/ai/tasks/combined.py +123 -0
  16. package/excel_word/wesync/ai/tasks/diff_note.py +61 -0
  17. package/excel_word/wesync/ai/tasks/headings.py +119 -0
  18. package/excel_word/wesync/ai/tasks/help_task.py +69 -0
  19. package/excel_word/wesync/ai/tasks/roles.py +128 -0
  20. package/excel_word/wesync/align.py +1 -0
  21. package/excel_word/wesync/api.py +544 -92
  22. package/excel_word/wesync/backup_history.py +44 -9
  23. package/excel_word/wesync/cli.py +19 -1
  24. package/excel_word/wesync/commands/blank_cmd.py +59 -30
  25. package/excel_word/wesync/commands/bookmark_cmd.py +41 -10
  26. package/excel_word/wesync/commands/clear_cmd.py +5 -3
  27. package/excel_word/wesync/commands/export_cmd.py +270 -220
  28. package/excel_word/wesync/commands/incremental_cmd.py +157 -0
  29. package/excel_word/wesync/commands/marks_cmd.py +24 -16
  30. package/excel_word/wesync/commands/placeholders_cmd.py +12 -7
  31. package/excel_word/wesync/commands/writeback_cmd.py +72 -116
  32. package/excel_word/wesync/config.py +3 -2
  33. package/excel_word/wesync/console.py +9 -2
  34. package/excel_word/wesync/docx_bookmarks.py +12 -5
  35. package/excel_word/wesync/docx_columns.py +1 -1
  36. package/excel_word/wesync/docx_marks.py +183 -75
  37. package/excel_word/wesync/docx_placeholders.py +65 -26
  38. package/excel_word/wesync/excel_blocks.py +144 -185
  39. package/excel_word/wesync/excel_export.py +15 -8
  40. package/excel_word/wesync/excel_marks.py +265 -193
  41. package/excel_word/wesync/fsops.py +11 -6
  42. package/excel_word/wesync/gui/__init__.py +21 -2
  43. package/excel_word/wesync/gui/app.py +243 -29
  44. package/excel_word/wesync/gui/manual.py +5 -3
  45. package/excel_word/wesync/gui/pages/__init__.py +53 -4
  46. package/excel_word/wesync/gui/pages/_page.py +129 -7
  47. package/excel_word/wesync/gui/pages/ai_page.py +854 -0
  48. package/excel_word/wesync/gui/pages/batch_page.py +64 -31
  49. package/excel_word/wesync/gui/pages/blank_page.py +529 -464
  50. package/excel_word/wesync/gui/pages/bookmark_page.py +97 -37
  51. package/excel_word/wesync/gui/pages/export_page.py +140 -87
  52. package/excel_word/wesync/gui/pages/feedback_page.py +196 -0
  53. package/excel_word/wesync/gui/pages/format_page.py +551 -429
  54. package/excel_word/wesync/gui/pages/help_page.py +73 -7
  55. package/excel_word/wesync/gui/pages/history_page.py +167 -144
  56. package/excel_word/wesync/gui/pages/marks_page.py +210 -177
  57. package/excel_word/wesync/gui/pages/settings_page.py +450 -0
  58. package/excel_word/wesync/gui/pages/writeback_page.py +167 -146
  59. package/excel_word/wesync/gui/runner.py +29 -13
  60. package/excel_word/wesync/gui/store.py +23 -7
  61. package/excel_word/wesync/gui/theme.py +220 -49
  62. package/excel_word/wesync/gui/widgets/ai_config.py +362 -0
  63. package/excel_word/wesync/gui/widgets/cards.py +124 -5
  64. package/excel_word/wesync/gui/widgets/chips.py +565 -0
  65. package/excel_word/wesync/gui/widgets/filebar.py +1 -1
  66. package/excel_word/wesync/gui/widgets/logpane.py +24 -8
  67. package/excel_word/wesync/gui/widgets/nav.py +267 -12
  68. package/excel_word/wesync/gui/widgets/theme_choice.py +93 -0
  69. package/excel_word/wesync/report_format.py +9 -1
  70. package/excel_word/wesync/settings.py +35 -0
  71. package/excel_word/wesync/sort_rules.py +75 -26
  72. package/excel_word/wesync/valuefmt.py +40 -17
  73. package/package.json +53 -51
  74. package/packaging//344/275/277/347/224/250/350/257/264/346/230/216.txt +247 -0
  75. package/qoder_plugins/audit-toolbox/README.md +159 -156
  76. package/qoder_plugins/audit-toolbox/catalog.mjs +13 -13
@@ -0,0 +1,157 @@
1
+ """增量导出:只登记 Word 中尚未锚定的表格、标注与占位符。"""
2
+
3
+ from collections import Counter
4
+
5
+ import docx
6
+
7
+ from ..config import BOOKMARK_PREFIX, SHEET_NAME, SHEET_NEW_TABLES
8
+ from ..console import log
9
+ from ..docx_bookmarks import bookmark_names, insert_table_bookmark, new_cpah_name, next_bookmark_id, table_index_map_by_bookmark
10
+ from ..docx_grid import body_items
11
+ from ..docx_marks import _duplicate_mark_bookmarks, ensure_mark_bookmarks, find_marks
12
+ from ..docx_meta import collect_table_info, dedupe_table_names
13
+ from ..docx_placeholders import append_placeholder_sheet, scan_placeholders
14
+ from ..errors import SyncError
15
+ from ..excel_blocks import parse_blocks
16
+ from ..excel_export import export_workbook
17
+ from ..excel_marks import append_marks_sheet, read_previous_marks
18
+ from ..fsops import check_file_free, load_workbook_for_save, resolve_docx, resolve_xlsx, save_docx_xlsx_transaction
19
+
20
+
21
+ def _load_blocks(wb):
22
+ order: list[str] = []
23
+ blocks: dict[str, dict] = {}
24
+ for sheet_name in (SHEET_NAME, SHEET_NEW_TABLES):
25
+ if sheet_name not in wb.sheetnames:
26
+ continue
27
+ sheet_order, sheet_blocks = parse_blocks(wb[sheet_name])
28
+ duplicate = set(sheet_order) & set(blocks)
29
+ if duplicate:
30
+ raise SyncError(f"工作簿中表编号重复:{'、'.join(sorted(duplicate))}。请修正后再增量导出。")
31
+ for code, block in sheet_blocks.items():
32
+ block["sheet_name"] = sheet_name
33
+ block["_ws"] = wb[sheet_name]
34
+ order.extend(sheet_order)
35
+ blocks.update(sheet_blocks)
36
+ return order, blocks
37
+
38
+
39
+ def _ignored_table_content(doc) -> tuple[int, int]:
40
+ from ..docx_marks import _paragraph_mark_highlighted, run_highlighted
41
+ from ..docx_placeholders import PLACEHOLDER_RE
42
+
43
+ marks = placeholders = 0
44
+ roots = [doc.element.body]
45
+ for section in doc.sections:
46
+ roots.extend(part._element for part in (
47
+ section.header, section.footer, section.first_page_header, section.first_page_footer,
48
+ section.even_page_header, section.even_page_footer,
49
+ ) if part is not None)
50
+ for root in roots:
51
+ for tc in root.iter(docx.oxml.ns.qn("w:tc")):
52
+ for p_el in tc.iter(docx.oxml.ns.qn("w:p")):
53
+ if _paragraph_mark_highlighted(p_el) or any(run_highlighted(r, doc) for r in p_el.iter(docx.oxml.ns.qn("w:r"))):
54
+ marks += 1
55
+ placeholders += len(PLACEHOLDER_RE.findall("".join(t.text or "" for t in p_el.iter(docx.oxml.ns.qn("w:t")))))
56
+ return marks, placeholders
57
+
58
+
59
+ def cmd_incremental_export(args) -> int:
60
+ docx_path = resolve_docx(args.docx)
61
+ xlsx_path = resolve_xlsx(docx_path, args.excel)
62
+ if not xlsx_path.exists():
63
+ raise SyncError(f"找不到工作簿:{xlsx_path}\n请先执行一次全量「导出表格到 Excel」。")
64
+ if not args.dry_run:
65
+ check_file_free(docx_path, args.force)
66
+ check_file_free(xlsx_path, args.force)
67
+
68
+ doc = docx.Document(str(docx_path))
69
+ wb = load_workbook_for_save(xlsx_path)
70
+ if SHEET_NAME not in wb.sheetnames:
71
+ raise SyncError(f"工作簿中找不到工作表“{SHEET_NAME}”,请确认该文件由本工具导出")
72
+ order, blocks = _load_blocks(wb)
73
+ previous_marks = read_previous_marks(xlsx_path)
74
+ known_mark_bookmarks = {row.get("bookmark") for row in previous_marks if row.get("bookmark")}
75
+
76
+ recorded = {code: (block.get("bookmark") or "").strip() for code, block in blocks.items()}
77
+ candidates = {name for name in recorded.values() if name}
78
+ table_index = table_index_map_by_bookmark(doc, candidates, include_anchors=True)
79
+ counts = Counter(bookmark_names(doc.element.body))
80
+ duplicates = [name for name in counts if name.startswith(BOOKMARK_PREFIX) and counts[name] > 1]
81
+ if duplicates:
82
+ raise SyncError(f"Word 中存在重复的表格书签:{'、'.join(sorted(duplicates))}。请先清理重复书签。")
83
+ _duplicate_mark_bookmarks(doc)
84
+
85
+ items = body_items(doc)
86
+ new_indices = [index for index in range(len(doc.tables)) if index not in set(table_index.values())]
87
+ all_infos = [collect_table_info(doc, items, index, tab) for index, tab in enumerate(doc.tables)]
88
+ dedupe_table_names(all_infos)
89
+ existing_names = bookmark_names(doc.element.body)
90
+ bookmark_id = next_bookmark_id(doc)
91
+ new_infos = []
92
+ next_number = max([int(code[2:]) for code in order if code.startswith("XT") and code[2:].isdigit()] + [0]) + 1
93
+ for offset, index in enumerate(new_indices):
94
+ info = dict(all_infos[index])
95
+ name = new_cpah_name(existing_names)
96
+ info["bookmark"] = name
97
+ info["code"] = f"XT{next_number + offset:02d}"
98
+ new_infos.append(info)
99
+
100
+ marks = [mark for mark in find_marks(doc) if not mark.bookmark or mark.bookmark not in known_mark_bookmarks]
101
+ fields = scan_placeholders(doc)
102
+ existing_fields = set()
103
+ from ..docx_placeholders import VALUES_SHEET
104
+ if VALUES_SHEET in wb.sheetnames:
105
+ existing_fields = {str(wb[VALUES_SHEET].cell(r, 1).value or "").strip() for r in range(2, (wb[VALUES_SHEET].max_row or 1) + 1)}
106
+ new_fields = [field for field in fields if field not in existing_fields]
107
+ ignored_marks, ignored_placeholders = _ignored_table_content(doc)
108
+
109
+ log(f"读取 Word:{docx_path.name}")
110
+ log(f"增量预演:新增表格 {len(new_infos)} 张、新增标注 {len(marks)} 处、新增占位符 {len(new_fields)} 个。")
111
+ for info in new_infos:
112
+ log(f" {info['code']} {info['name']}({info['nrows']}行×{info['ncols']}列,书签 {info['bookmark']})")
113
+ for mark in marks:
114
+ log(f" {mark.location}:{mark.text[:40]}")
115
+ if new_fields:
116
+ log(f" 新字段:{'、'.join(new_fields)}")
117
+ if ignored_marks or ignored_placeholders:
118
+ log(f"[忽略] 表格内高亮 {ignored_marks} 处、占位符 {ignored_placeholders} 个不导出,避免与表格同步冲突。")
119
+ if args.dry_run:
120
+ log("预演模式:未写入任何文件。")
121
+ wb.close()
122
+ if getattr(wb, "vba_archive", None):
123
+ wb.vba_archive.close()
124
+ return 0
125
+
126
+ if not new_infos and not marks and not new_fields:
127
+ log("增量预演:没有新增表格、标注或占位符,未修改任何文件。")
128
+ wb.close()
129
+ if getattr(wb, "vba_archive", None):
130
+ wb.vba_archive.close()
131
+ return 0
132
+
133
+ for info in new_infos:
134
+ insert_table_bookmark(doc.tables[info["index"]]._tbl, info["bookmark"], bookmark_id)
135
+ bookmark_id += 1
136
+ created_marks = ensure_mark_bookmarks(doc, marks)
137
+ if new_infos:
138
+ export_workbook(
139
+ new_infos, docx_path.name, with_links=not getattr(args, "no_excel_links", False),
140
+ totals_formula=not getattr(args, "no_total_formulas", False), wb=wb, sheet_name=SHEET_NEW_TABLES,
141
+ )
142
+ mark_stats = append_marks_sheet(wb, docx_path.name, marks, previous_marks) if marks else {"marks": 0}
143
+ added_fields = append_placeholder_sheet(wb, new_fields, source="增量") if new_fields else []
144
+ backups = save_docx_xlsx_transaction(
145
+ docx_path=docx_path if new_infos or created_marks else None,
146
+ doc=doc if new_infos or created_marks else None,
147
+ xlsx_path=xlsx_path, wb=wb, backup=getattr(args, "backup", True),
148
+ )
149
+ log(
150
+ f"[增量导出] 已登记表格 {len(new_infos)} 张、标注 {mark_stats['marks']} 处、占位符 {len(added_fields)} 个;"
151
+ "既有工作簿行与公式位置未改动。"
152
+ )
153
+ if backups.get(docx_path):
154
+ log(f"[增量导出] Word 已备份:{backups[docx_path].name}")
155
+ if backups.get(xlsx_path):
156
+ log(f"[增量导出] Excel 已备份:{backups[xlsx_path].name}")
157
+ return 0
@@ -13,7 +13,7 @@ from ..docx_marks import (
13
13
  )
14
14
  from ..errors import SyncError
15
15
  from ..excel_marks import SHEET_MARKS, build_marks_sheet, parse_marks_rows, read_previous_marks
16
- from ..fsops import backup_file, check_file_free, load_workbook_for_save, resolve_docx, resolve_xlsx
16
+ from ..fsops import check_file_free, load_workbook_for_save, resolve_docx, resolve_xlsx, save_docx_xlsx_transaction
17
17
 
18
18
 
19
19
  def _mark_label(bookmark: str, fallback: str) -> str:
@@ -52,17 +52,16 @@ def cmd_marks(args) -> int:
52
52
  return 0
53
53
 
54
54
  created = ensure_mark_bookmarks(doc, marks)
55
- if created:
56
- backup = backup_file(docx_path)
57
- doc.save(str(docx_path))
58
- log(f"[标记] 已为 {created} 处标记插入定位书签并保存 Word(备份:{backup.name if backup else '无'})")
59
- else:
60
- log("[标记] 所有标记都已有定位书签,Word 无需改动")
61
-
62
55
  workbook = load_workbook_for_save(xlsx_path)
63
56
  stats = build_marks_sheet(workbook, docx_path.name, marks, previous, processed)
64
- xlsx_backup = backup_file(xlsx_path)
65
- workbook.save(str(xlsx_path))
57
+ backups = save_docx_xlsx_transaction(
58
+ docx_path=docx_path if created else None, doc=doc if created else None,
59
+ xlsx_path=xlsx_path, wb=workbook, backup=getattr(args, "backup", True),
60
+ )
61
+ docx_backup = backups.get(docx_path) if created else None
62
+ xlsx_backup = backups.get(xlsx_path)
63
+ if created:
64
+ log(f"[标记] 已为 {created} 处标记插入定位书签并保存 Word(备份:{docx_backup.name if docx_backup else '无'})")
66
65
  log(
67
66
  f"[标记] 已刷新工作表「{SHEET_MARKS}」:{stats['marks']} 行标记"
68
67
  + (f",{stats['processed']} 行已处理" if stats.get("processed") else "")
@@ -88,12 +87,15 @@ def cmd_marks_sync(args) -> int:
88
87
  rows = getattr(args, "rows", None)
89
88
  if rows is None:
90
89
  cached_workbook = load_workbook(str(xlsx_path), data_only=True)
91
- rows = parse_marks_rows(workbook[SHEET_MARKS], cached_workbook[SHEET_MARKS])
90
+ rows = parse_marks_rows(
91
+ workbook[SHEET_MARKS], cached_workbook[SHEET_MARKS], zero_mode=getattr(args, "mark_zero", "excel")
92
+ )
92
93
  if not rows:
93
94
  raise SyncError(f"「{SHEET_MARKS}」工作表里没有数据行,请先执行“导出/刷新标注”。")
94
95
 
95
- doc = docx.Document(str(docx_path))
96
+ doc = getattr(args, "_doc", None) or docx.Document(str(docx_path))
96
97
  keep_highlight = bool(getattr(args, "keep_highlight", False))
98
+ mark_parts = {mark.bookmark: mark.part for mark in find_marks(doc) if mark.bookmark}
97
99
  log(f"读取 Word:{docx_path.name}")
98
100
  log(f"读取 Excel:{xlsx_path.name}(工作表:{SHEET_MARKS},{len(rows)} 行)")
99
101
  if keep_highlight:
@@ -116,13 +118,14 @@ def cmd_marks_sync(args) -> int:
116
118
  deleted += 1
117
119
  log(f"[跳过] {label} {row['original'][:24]}:Word 中该标记已删除")
118
120
  continue
119
- if not row["new"] and not row["link"]:
121
+ if not row.get("new_filled", bool(row["new"])) and not row["link"]:
120
122
  pending += 1
121
123
  warn(f"{label} {row['position']}:新内容与链接地址均未填,保留原样未同步。")
122
124
  continue
123
125
  p_el, children, runs = payload
126
+ part = mark_parts.get(bookmark, doc.part)
124
127
  result = apply_mark_change(
125
- doc.part, p_el, children, runs, row["new"], row["link"], keep_highlight=keep_highlight
128
+ part, p_el, children, runs, row["new"], row["link"], keep_highlight=keep_highlight, doc=doc
126
129
  )
127
130
  if result == "deleted":
128
131
  deleted += 1
@@ -146,7 +149,12 @@ def cmd_marks_sync(args) -> int:
146
149
  if not changed:
147
150
  log("标注表与 Word 内容一致,未做任何写入。")
148
151
  return 0
149
- backup = backup_file(docx_path)
150
- doc.save(str(docx_path))
152
+ if getattr(args, "_skip_save", False):
153
+ args._marks_changed = True
154
+ return 0
155
+ backups = save_docx_xlsx_transaction(
156
+ docx_path=docx_path, doc=doc, backup=getattr(args, "backup", True),
157
+ )
158
+ backup = backups.get(docx_path)
151
159
  log(f"[标记] 已保存 {docx_path.name}" + (f"(原文件已备份:{backup.name})" if backup else ""))
152
160
  return 0
@@ -11,7 +11,7 @@ from ..docx_placeholders import (
11
11
  write_placeholder_sheet,
12
12
  )
13
13
  from ..errors import SyncError
14
- from ..fsops import backup_file, check_file_free, resolve_docx, resolve_xlsx
14
+ from ..fsops import check_file_free, resolve_docx, resolve_xlsx, save_docx_xlsx_transaction
15
15
 
16
16
 
17
17
  def cmd_placeholders(args) -> int:
@@ -24,7 +24,7 @@ def cmd_placeholders(args) -> int:
24
24
  )
25
25
  if not args.dry_run:
26
26
  check_file_free(xlsx_path, args.force)
27
- doc = docx.Document(str(docx_path))
27
+ doc = getattr(args, "_doc", None) or docx.Document(str(docx_path))
28
28
  fields = scan_placeholders(doc)
29
29
  log(f"读取 Word:{docx_path.name}")
30
30
  if not fields:
@@ -34,7 +34,7 @@ def cmd_placeholders(args) -> int:
34
34
  if args.dry_run:
35
35
  log("预演模式(--dry-run):以上字段将加入/补齐「正文信息」表,未写入任何文件。")
36
36
  return 0
37
- added = write_placeholder_sheet(xlsx_path, fields)
37
+ added = write_placeholder_sheet(xlsx_path, fields, backup=getattr(args, "backup", True))
38
38
  log(
39
39
  f"[占位符] 「{VALUES_SHEET}」表已就绪({len(fields)} 个字段"
40
40
  + (f",本次新增 {len(added)} 个" if added else ",无新增字段")
@@ -54,8 +54,8 @@ def cmd_placeholders_sync(args) -> int:
54
54
  check_file_free(xlsx_path, args.force)
55
55
  values = getattr(args, "values", None)
56
56
  if values is None:
57
- values = read_placeholder_values(xlsx_path)
58
- doc = docx.Document(str(docx_path))
57
+ values = read_placeholder_values(xlsx_path, zero_mode=getattr(args, "placeholder_zero", "excel"))
58
+ doc = getattr(args, "_doc", None) or docx.Document(str(docx_path))
59
59
  fields = scan_placeholders(doc)
60
60
  log(f"读取 Word:{docx_path.name}")
61
61
  if not fields:
@@ -81,7 +81,12 @@ def cmd_placeholders_sync(args) -> int:
81
81
  if not result["replaced"]:
82
82
  log("没有可替换的占位符,未做任何写入。")
83
83
  return 0
84
- backup = backup_file(docx_path)
85
- doc.save(str(docx_path))
84
+ if getattr(args, "_skip_save", False):
85
+ args._placeholder_changed = bool(result["replaced"])
86
+ return 0
87
+ backups = save_docx_xlsx_transaction(
88
+ docx_path=docx_path, doc=doc, backup=getattr(args, "backup", True),
89
+ )
90
+ backup = backups.get(docx_path)
86
91
  log(f"[占位符] 已保存 {docx_path.name}" + (f"(原文件已备份:{backup.name})" if backup else ""))
87
92
  return 0