audit-e2w 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +152 -3
- package/cordis.patch.yml +3 -0
- package/dsh_plugins/audit-e2w/lib/engine.mjs +174 -0
- package/dsh_plugins/audit-e2w/lib/index.js +122 -0
- package/dsh_plugins/audit-e2w/lib/skill.js +42 -0
- package/excel_word/README.md +388 -0
- package/excel_word/qoder_engine.py +437 -0
- package/excel_word/requirements.txt +4 -0
- package/excel_word/wesync/__init__.py +19 -0
- package/excel_word/wesync/__main__.py +6 -0
- package/excel_word/wesync/ai/__init__.py +52 -0
- package/excel_word/wesync/ai/actions.py +369 -0
- package/excel_word/wesync/ai/client.py +340 -0
- package/excel_word/wesync/ai/config.py +291 -0
- package/excel_word/wesync/ai/docs.py +177 -0
- package/excel_word/wesync/ai/prompts.py +288 -0
- package/excel_word/wesync/ai/tasks/__init__.py +26 -0
- package/excel_word/wesync/ai/tasks/actions_task.py +97 -0
- package/excel_word/wesync/ai/tasks/combined.py +123 -0
- package/excel_word/wesync/ai/tasks/diff_note.py +61 -0
- package/excel_word/wesync/ai/tasks/headings.py +119 -0
- package/excel_word/wesync/ai/tasks/help_task.py +69 -0
- package/excel_word/wesync/ai/tasks/roles.py +128 -0
- package/excel_word/wesync/align.py +155 -0
- package/excel_word/wesync/api.py +1292 -0
- package/excel_word/wesync/backup_history.py +100 -0
- package/excel_word/wesync/changelog.py +58 -0
- package/excel_word/wesync/cli.py +191 -0
- package/excel_word/wesync/commands/__init__.py +8 -0
- package/excel_word/wesync/commands/blank_cmd.py +279 -0
- package/excel_word/wesync/commands/bookmark_cmd.py +146 -0
- package/excel_word/wesync/commands/clear_cmd.py +50 -0
- package/excel_word/wesync/commands/export_cmd.py +270 -0
- package/excel_word/wesync/commands/incremental_cmd.py +157 -0
- package/excel_word/wesync/commands/marks_cmd.py +160 -0
- package/excel_word/wesync/commands/placeholders_cmd.py +92 -0
- package/excel_word/wesync/commands/writeback_cmd.py +469 -0
- package/excel_word/wesync/config.py +97 -0
- package/excel_word/wesync/console.py +85 -0
- package/excel_word/wesync/docx_blank.py +156 -0
- package/excel_word/wesync/docx_bookmarks.py +294 -0
- package/excel_word/wesync/docx_columns.py +257 -0
- package/excel_word/wesync/docx_edit.py +77 -0
- package/excel_word/wesync/docx_grid.py +212 -0
- package/excel_word/wesync/docx_marks.py +399 -0
- package/excel_word/wesync/docx_meta.py +157 -0
- package/excel_word/wesync/docx_newtable.py +220 -0
- package/excel_word/wesync/docx_placeholders.py +200 -0
- package/excel_word/wesync/docx_rows.py +67 -0
- package/excel_word/wesync/errors.py +5 -0
- package/excel_word/wesync/excel_blocks.py +502 -0
- package/excel_word/wesync/excel_export.py +143 -0
- package/excel_word/wesync/excel_marks.py +265 -0
- package/excel_word/wesync/excel_totals.py +53 -0
- package/excel_word/wesync/fsops.py +219 -0
- package/excel_word/wesync/gui/__init__.py +53 -0
- package/excel_word/wesync/gui/__main__.py +6 -0
- package/excel_word/wesync/gui/app.py +613 -0
- package/excel_word/wesync/gui/manual.py +70 -0
- package/excel_word/wesync/gui/pages/__init__.py +80 -0
- package/excel_word/wesync/gui/pages/_page.py +211 -0
- package/excel_word/wesync/gui/pages/ai_page.py +854 -0
- package/excel_word/wesync/gui/pages/batch_page.py +456 -0
- package/excel_word/wesync/gui/pages/blank_page.py +530 -0
- package/excel_word/wesync/gui/pages/bookmark_page.py +97 -0
- package/excel_word/wesync/gui/pages/export_page.py +141 -0
- package/excel_word/wesync/gui/pages/feedback_page.py +196 -0
- package/excel_word/wesync/gui/pages/format_page.py +551 -0
- package/excel_word/wesync/gui/pages/help_page.py +158 -0
- package/excel_word/wesync/gui/pages/history_page.py +168 -0
- package/excel_word/wesync/gui/pages/marks_page.py +211 -0
- package/excel_word/wesync/gui/pages/settings_page.py +450 -0
- package/excel_word/wesync/gui/pages/writeback_page.py +473 -0
- package/excel_word/wesync/gui/resources.py +22 -0
- package/excel_word/wesync/gui/runner.py +90 -0
- package/excel_word/wesync/gui/store.py +50 -0
- package/excel_word/wesync/gui/theme.py +271 -0
- package/excel_word/wesync/gui/widgets/__init__.py +8 -0
- package/excel_word/wesync/gui/widgets/ai_config.py +362 -0
- package/excel_word/wesync/gui/widgets/cards.py +139 -0
- package/excel_word/wesync/gui/widgets/chips.py +565 -0
- package/excel_word/wesync/gui/widgets/filebar.py +58 -0
- package/excel_word/wesync/gui/widgets/logpane.py +126 -0
- package/excel_word/wesync/gui/widgets/nav.py +294 -0
- package/excel_word/wesync/gui/widgets/theme_choice.py +93 -0
- package/excel_word/wesync/report_format.py +1585 -0
- package/excel_word/wesync/settings.py +35 -0
- package/excel_word/wesync/sort_rules.py +293 -0
- package/excel_word/wesync/valuefmt.py +485 -0
- package/package.json +53 -6
- package/packaging//344/275/277/347/224/250/350/257/264/346/230/216.txt +247 -0
- package/qoder_plugins/audit-toolbox/README.md +159 -0
- package/qoder_plugins/audit-toolbox/catalog.mjs +265 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
"""高亮标记层:识别显式高亮并建立可定位、可写回的书签锚点。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
|
|
8
|
+
from docx.oxml import OxmlElement
|
|
9
|
+
from docx.oxml.ns import qn
|
|
10
|
+
from docx.opc.constants import RELATIONSHIP_TYPE as RT
|
|
11
|
+
|
|
12
|
+
from .docx_bookmarks import bookmark_names, next_bookmark_id
|
|
13
|
+
from .errors import SyncError
|
|
14
|
+
|
|
15
|
+
MARK_PREFIX = "MRK_"
|
|
16
|
+
MARK_NAME_RE = re.compile(rf"^{MARK_PREFIX}\d{{4}}$")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class Mark:
|
|
21
|
+
index: int
|
|
22
|
+
p_el: object
|
|
23
|
+
children: list = field(default_factory=list)
|
|
24
|
+
runs: list = field(default_factory=list)
|
|
25
|
+
bookmark: str = ""
|
|
26
|
+
text: str = ""
|
|
27
|
+
context: str = ""
|
|
28
|
+
location: str = ""
|
|
29
|
+
part: object = None
|
|
30
|
+
doc: object = None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _highlight_val(r_el) -> str | None:
|
|
34
|
+
rPr = r_el.find(qn("w:rPr"))
|
|
35
|
+
if rPr is None:
|
|
36
|
+
return None
|
|
37
|
+
hl = rPr.find(qn("w:highlight"))
|
|
38
|
+
if hl is None:
|
|
39
|
+
return None
|
|
40
|
+
return hl.get(qn("w:val"))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _style_highlight_val(doc, style_id: str | None) -> str | None:
|
|
44
|
+
if doc is None or not style_id:
|
|
45
|
+
return None
|
|
46
|
+
try:
|
|
47
|
+
styles = doc.styles.element
|
|
48
|
+
except Exception:
|
|
49
|
+
return None
|
|
50
|
+
style = next((s for s in styles.findall(qn("w:style")) if s.get(qn("w:styleId")) == style_id), None)
|
|
51
|
+
if style is None or style.get(qn("w:type")) != "character":
|
|
52
|
+
return None
|
|
53
|
+
seen = set()
|
|
54
|
+
while style is not None and style.get(qn("w:type")) == "character":
|
|
55
|
+
sid = style.get(qn("w:styleId"))
|
|
56
|
+
if sid in seen:
|
|
57
|
+
return None
|
|
58
|
+
seen.add(sid)
|
|
59
|
+
rPr = style.find(qn("w:rPr"))
|
|
60
|
+
hl = rPr.find(qn("w:highlight")) if rPr is not None else None
|
|
61
|
+
if hl is not None:
|
|
62
|
+
return hl.get(qn("w:val"))
|
|
63
|
+
based = style.find(qn("w:basedOn"))
|
|
64
|
+
parent_id = based.get(qn("w:val")) if based is not None else None
|
|
65
|
+
style = next((s for s in styles.findall(qn("w:style")) if s.get(qn("w:styleId")) == parent_id), None)
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def run_highlighted(r_el, doc=None) -> bool:
|
|
70
|
+
value = _highlight_val(r_el)
|
|
71
|
+
if value is not None:
|
|
72
|
+
return value != "none"
|
|
73
|
+
rPr = r_el.find(qn("w:rPr"))
|
|
74
|
+
style_id = rPr.find(qn("w:rStyle")).get(qn("w:val")) if rPr is not None and rPr.find(qn("w:rStyle")) is not None else None
|
|
75
|
+
return _style_highlight_val(doc, style_id) not in (None, "none")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _style_highlighted(r_el, doc) -> bool:
|
|
79
|
+
rPr = r_el.find(qn("w:rPr"))
|
|
80
|
+
style_id = rPr.find(qn("w:rStyle")).get(qn("w:val")) if rPr is not None and rPr.find(qn("w:rStyle")) is not None else None
|
|
81
|
+
return _highlight_val(r_el) is None and _style_highlight_val(doc, style_id) not in (None, "none")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _paragraph_mark_highlighted(p_el) -> bool:
|
|
85
|
+
pPr = p_el.find(qn("w:pPr"))
|
|
86
|
+
rPr = pPr.find(qn("w:rPr")) if pPr is not None else None
|
|
87
|
+
hl = rPr.find(qn("w:highlight")) if rPr is not None else None
|
|
88
|
+
return hl is not None and hl.get(qn("w:val")) != "none"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _inside_table(p_el) -> bool:
|
|
92
|
+
parent = p_el.getparent()
|
|
93
|
+
while parent is not None:
|
|
94
|
+
if parent.tag == qn("w:tc"):
|
|
95
|
+
return True
|
|
96
|
+
parent = parent.getparent()
|
|
97
|
+
return False
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _run_texts(runs: list) -> str:
|
|
101
|
+
return "".join("".join(t.text or "" for t in r.iter(qn("w:t"))) for r in runs)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _containers(doc):
|
|
105
|
+
seen: set[int] = set()
|
|
106
|
+
yield doc.element.body, "body"
|
|
107
|
+
seen.add(id(doc.element.body))
|
|
108
|
+
for section_index, section in enumerate(doc.sections, 1):
|
|
109
|
+
for label, part in (
|
|
110
|
+
("页眉", section.header),
|
|
111
|
+
("页脚", section.footer),
|
|
112
|
+
("首页页眉", section.first_page_header),
|
|
113
|
+
("首页页脚", section.first_page_footer),
|
|
114
|
+
("偶数页页眉", section.even_page_header),
|
|
115
|
+
("偶数页页脚", section.even_page_footer),
|
|
116
|
+
):
|
|
117
|
+
if part is not None and id(part._element) not in seen:
|
|
118
|
+
seen.add(id(part._element))
|
|
119
|
+
yield part._element, f"第{section_index}节{label}"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _bookmark_ranges_in_paragraph(p_el, prefix: str = "") -> list[tuple[str, int, int]]:
|
|
123
|
+
children = list(p_el.iterchildren())
|
|
124
|
+
starts: dict[str, tuple[str, int]] = {}
|
|
125
|
+
ranges: list[tuple[str, int, int]] = []
|
|
126
|
+
for index, child in enumerate(children):
|
|
127
|
+
if child.tag == qn("w:bookmarkStart"):
|
|
128
|
+
name = child.get(qn("w:name")) or ""
|
|
129
|
+
if not prefix or name.startswith(prefix):
|
|
130
|
+
starts[child.get(qn("w:id"))] = (name, index)
|
|
131
|
+
elif child.tag == qn("w:bookmarkEnd"):
|
|
132
|
+
key = child.get(qn("w:id"))
|
|
133
|
+
if key in starts:
|
|
134
|
+
name, start = starts.pop(key)
|
|
135
|
+
ranges.append((name, start, index))
|
|
136
|
+
return ranges
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _duplicate_mark_bookmarks(doc) -> dict[str, int]:
|
|
140
|
+
counts: dict[str, int] = {}
|
|
141
|
+
for root, _label in _containers(doc):
|
|
142
|
+
for start in root.iter(qn("w:bookmarkStart")):
|
|
143
|
+
name = start.get(qn("w:name")) or ""
|
|
144
|
+
if MARK_NAME_RE.match(name):
|
|
145
|
+
counts[name] = counts.get(name, 0) + 1
|
|
146
|
+
duplicates = {name: count for name, count in counts.items() if count > 1}
|
|
147
|
+
if duplicates:
|
|
148
|
+
detail = "、".join(f"{name}({count} 处)" for name, count in sorted(duplicates.items()))
|
|
149
|
+
raise SyncError(f"Word 中存在重复的标注书签:{detail}。请先清理重复书签,再导出或同步。")
|
|
150
|
+
return counts
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _payload_of_child(child, doc):
|
|
154
|
+
if child.tag == qn("w:r"):
|
|
155
|
+
return [child] if run_highlighted(child, doc) else []
|
|
156
|
+
if child.tag == qn("w:hyperlink"):
|
|
157
|
+
runs = [r for r in child.iter(qn("w:r"))]
|
|
158
|
+
if runs and all(run_highlighted(r, doc) for r in runs):
|
|
159
|
+
return runs
|
|
160
|
+
return []
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _scan_paragraph(p_el, part, doc, location: str, marks: list[Mark]) -> None:
|
|
164
|
+
if _inside_table(p_el):
|
|
165
|
+
return
|
|
166
|
+
children = list(p_el.iterchildren())
|
|
167
|
+
existing = {start: name for name, start, _end in _bookmark_ranges_in_paragraph(p_el, MARK_PREFIX)}
|
|
168
|
+
paragraph_text = "".join(t.text or "" for t in p_el.iter(qn("w:t"))).strip()
|
|
169
|
+
|
|
170
|
+
group: list = []
|
|
171
|
+
group_start = 0
|
|
172
|
+
paragraph_mark = _paragraph_mark_highlighted(p_el)
|
|
173
|
+
for index, child in enumerate(children):
|
|
174
|
+
payload = _payload_of_child(child, doc)
|
|
175
|
+
if paragraph_mark and child.tag in (qn("w:r"), qn("w:hyperlink")):
|
|
176
|
+
payload = list(child.iter(qn("w:r")))
|
|
177
|
+
if payload:
|
|
178
|
+
if not group:
|
|
179
|
+
group_start = index
|
|
180
|
+
group.append((child, payload))
|
|
181
|
+
continue
|
|
182
|
+
if group and not paragraph_mark:
|
|
183
|
+
_emit_mark(p_el, part, doc, children, group, group_start, index - 1, location, paragraph_text, existing, marks)
|
|
184
|
+
group = []
|
|
185
|
+
if group:
|
|
186
|
+
_emit_mark(p_el, part, doc, children, group, group_start, len(children) - 1, location, paragraph_text, existing, marks)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _emit_mark(p_el, part, doc, children, group, start_index, end_index, location, paragraph_text, existing, marks) -> None:
|
|
190
|
+
runs: list = []
|
|
191
|
+
for _child, payload in group:
|
|
192
|
+
runs.extend(payload)
|
|
193
|
+
text = _run_texts(runs)
|
|
194
|
+
if not text.strip():
|
|
195
|
+
return
|
|
196
|
+
mark = Mark(
|
|
197
|
+
index=len(marks) + 1,
|
|
198
|
+
p_el=p_el,
|
|
199
|
+
children=[child for child, _payload in group],
|
|
200
|
+
runs=runs,
|
|
201
|
+
text=text,
|
|
202
|
+
context=paragraph_text[:80],
|
|
203
|
+
location=location,
|
|
204
|
+
part=part,
|
|
205
|
+
doc=doc,
|
|
206
|
+
)
|
|
207
|
+
name = existing.get(start_index - 1)
|
|
208
|
+
if name:
|
|
209
|
+
mark.bookmark = name
|
|
210
|
+
marks.append(mark)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def find_marks(doc) -> list[Mark]:
|
|
214
|
+
"""按文档顺序找出正文、页眉页脚里的高亮;表格内高亮不参与。"""
|
|
215
|
+
_duplicate_mark_bookmarks(doc)
|
|
216
|
+
marks: list[Mark] = []
|
|
217
|
+
para_no = 0
|
|
218
|
+
for root, label in _containers(doc):
|
|
219
|
+
for child in root.iterchildren():
|
|
220
|
+
if child.tag != qn("w:p"):
|
|
221
|
+
continue
|
|
222
|
+
if label == "body":
|
|
223
|
+
para_no += 1
|
|
224
|
+
location = f"正文 第{para_no}段"
|
|
225
|
+
else:
|
|
226
|
+
location = label
|
|
227
|
+
_scan_paragraph(child, _part_for_root(doc, root), doc, location, marks)
|
|
228
|
+
return marks
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _part_for_root(doc, root):
|
|
232
|
+
if root is doc.element.body:
|
|
233
|
+
return doc.part
|
|
234
|
+
for section in doc.sections:
|
|
235
|
+
for part in (section.header, section.footer, section.first_page_header,
|
|
236
|
+
section.first_page_footer, section.even_page_header, section.even_page_footer):
|
|
237
|
+
if part is not None and part._element is root:
|
|
238
|
+
return part.part
|
|
239
|
+
return doc.part
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def iter_paragraph_locations(doc):
|
|
243
|
+
"""按文档顺序产出正文与页眉页脚段落,表格内段落不产出。"""
|
|
244
|
+
para_no = 0
|
|
245
|
+
for root, label in _containers(doc):
|
|
246
|
+
for child in root.iterchildren():
|
|
247
|
+
if child.tag != qn("w:p"):
|
|
248
|
+
continue
|
|
249
|
+
if label == "body":
|
|
250
|
+
para_no += 1
|
|
251
|
+
yield child, f"正文 第{para_no}段"
|
|
252
|
+
else:
|
|
253
|
+
yield child, label
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def processed_marks(doc, pending: set[str]) -> list[dict]:
|
|
257
|
+
"""找出仍有 MRK 书签但已不高亮的标记。"""
|
|
258
|
+
result: list[dict] = []
|
|
259
|
+
for p_el, location in iter_paragraph_locations(doc):
|
|
260
|
+
for name, start, end in _bookmark_ranges_in_paragraph(p_el, MARK_PREFIX):
|
|
261
|
+
if not name or name in pending:
|
|
262
|
+
continue
|
|
263
|
+
children = list(p_el.iterchildren())[start + 1:end]
|
|
264
|
+
text = "".join("".join(t.text or "" for t in child.iter(qn("w:t"))) for child in children)
|
|
265
|
+
result.append({"bookmark": name, "text": text.strip(), "location": location})
|
|
266
|
+
return result
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def ensure_mark_bookmarks(doc, marks: list[Mark]) -> int:
|
|
270
|
+
_duplicate_mark_bookmarks(doc)
|
|
271
|
+
existing = set()
|
|
272
|
+
for root, _label in _containers(doc):
|
|
273
|
+
existing |= bookmark_names(root)
|
|
274
|
+
numbers = [int(name[len(MARK_PREFIX):]) for name in existing if MARK_NAME_RE.match(name)]
|
|
275
|
+
next_no = (max(numbers) + 1) if numbers else 1
|
|
276
|
+
bookmark_id = next_bookmark_id(doc)
|
|
277
|
+
created = 0
|
|
278
|
+
for mark in marks:
|
|
279
|
+
if mark.bookmark:
|
|
280
|
+
continue
|
|
281
|
+
name = f"{MARK_PREFIX}{next_no:04d}"
|
|
282
|
+
while name in existing:
|
|
283
|
+
next_no += 1
|
|
284
|
+
name = f"{MARK_PREFIX}{next_no:04d}"
|
|
285
|
+
next_no += 1
|
|
286
|
+
start = OxmlElement("w:bookmarkStart")
|
|
287
|
+
start.set(qn("w:id"), str(bookmark_id))
|
|
288
|
+
start.set(qn("w:name"), name)
|
|
289
|
+
end = OxmlElement("w:bookmarkEnd")
|
|
290
|
+
end.set(qn("w:id"), str(bookmark_id))
|
|
291
|
+
first = mark.children[0]
|
|
292
|
+
last = mark.children[-1]
|
|
293
|
+
first.addprevious(start)
|
|
294
|
+
last.addnext(end)
|
|
295
|
+
mark.bookmark = name
|
|
296
|
+
existing.add(name)
|
|
297
|
+
bookmark_id += 1
|
|
298
|
+
created += 1
|
|
299
|
+
return created
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def mark_payload_by_bookmark(doc, name: str):
|
|
303
|
+
_duplicate_mark_bookmarks(doc)
|
|
304
|
+
for p_el, _location in iter_paragraph_locations(doc):
|
|
305
|
+
for range_name, start, end in _bookmark_ranges_in_paragraph(p_el):
|
|
306
|
+
if range_name != name:
|
|
307
|
+
continue
|
|
308
|
+
children = list(p_el.iterchildren())[start + 1:end]
|
|
309
|
+
runs: list = []
|
|
310
|
+
for child in children:
|
|
311
|
+
if child.tag == qn("w:r"):
|
|
312
|
+
runs.append(child)
|
|
313
|
+
elif child.tag == qn("w:hyperlink"):
|
|
314
|
+
runs.extend(child.iter(qn("w:r")))
|
|
315
|
+
return p_el, children, runs
|
|
316
|
+
return None
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def _remove_highlight(r_el) -> None:
|
|
320
|
+
rPr = r_el.find(qn("w:rPr"))
|
|
321
|
+
if rPr is None:
|
|
322
|
+
return
|
|
323
|
+
hl = rPr.find(qn("w:highlight"))
|
|
324
|
+
if hl is not None:
|
|
325
|
+
rPr.remove(hl)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _add_no_highlight(r_el) -> None:
|
|
329
|
+
rPr = r_el.find(qn("w:rPr"))
|
|
330
|
+
if rPr is None:
|
|
331
|
+
rPr = OxmlElement("w:rPr")
|
|
332
|
+
r_el.insert(0, rPr)
|
|
333
|
+
hl = rPr.find(qn("w:highlight"))
|
|
334
|
+
if hl is None:
|
|
335
|
+
hl = OxmlElement("w:highlight")
|
|
336
|
+
rPr.append(hl)
|
|
337
|
+
hl.set(qn("w:val"), "none")
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def apply_mark_change(part, p_el, children: list, runs: list, new_text: str, link: str,
|
|
341
|
+
keep_highlight: bool = False, doc=None) -> str:
|
|
342
|
+
"""替换标记文字,按需加链接与去色;返回 changed/unchanged/deleted。"""
|
|
343
|
+
if not runs:
|
|
344
|
+
return "deleted"
|
|
345
|
+
current_text = _run_texts(runs)
|
|
346
|
+
target_text = new_text if new_text != "" else current_text
|
|
347
|
+
if new_text == "" and link == "":
|
|
348
|
+
return "unchanged"
|
|
349
|
+
|
|
350
|
+
existing_link = ""
|
|
351
|
+
for child in children:
|
|
352
|
+
if child.tag == qn("w:hyperlink"):
|
|
353
|
+
rel_id = child.get(qn("r:id"))
|
|
354
|
+
rel = part.rels.get(rel_id) if rel_id else None
|
|
355
|
+
existing_link = rel.target_ref if rel is not None else ""
|
|
356
|
+
break
|
|
357
|
+
|
|
358
|
+
style_highlight = any(_style_highlighted(r, doc) for r in runs)
|
|
359
|
+
highlight_pending = (not keep_highlight) and any(run_highlighted(r, doc) for r in runs)
|
|
360
|
+
changed = current_text != target_text or (link or "") != (existing_link or "") or highlight_pending
|
|
361
|
+
if not changed:
|
|
362
|
+
return "unchanged"
|
|
363
|
+
|
|
364
|
+
for child in list(children):
|
|
365
|
+
if child.tag == qn("w:hyperlink"):
|
|
366
|
+
for r in list(child.iter(qn("w:r"))):
|
|
367
|
+
if r.getparent() is child:
|
|
368
|
+
child.addprevious(r)
|
|
369
|
+
p_el.remove(child)
|
|
370
|
+
runs = [r for r in runs if r.getparent() is not None]
|
|
371
|
+
if not runs:
|
|
372
|
+
return "deleted"
|
|
373
|
+
|
|
374
|
+
keep = runs[0]
|
|
375
|
+
texts = keep.findall(qn("w:t"))
|
|
376
|
+
for index, node in enumerate(texts):
|
|
377
|
+
node.text = target_text if index == 0 else ""
|
|
378
|
+
node.set(qn("xml:space"), "preserve")
|
|
379
|
+
if not texts:
|
|
380
|
+
node = OxmlElement("w:t")
|
|
381
|
+
node.text = target_text
|
|
382
|
+
node.set(qn("xml:space"), "preserve")
|
|
383
|
+
keep.append(node)
|
|
384
|
+
if not keep_highlight:
|
|
385
|
+
_remove_highlight(keep)
|
|
386
|
+
if style_highlight:
|
|
387
|
+
_add_no_highlight(keep)
|
|
388
|
+
for extra in runs[1:]:
|
|
389
|
+
parent = extra.getparent()
|
|
390
|
+
if parent is not None:
|
|
391
|
+
parent.remove(extra)
|
|
392
|
+
|
|
393
|
+
if link:
|
|
394
|
+
hyperlink = OxmlElement("w:hyperlink")
|
|
395
|
+
hyperlink.set(qn("r:id"), part.relate_to(link, RT.HYPERLINK, is_external=True))
|
|
396
|
+
hyperlink.set(qn("w:history"), "1")
|
|
397
|
+
keep.addprevious(hyperlink)
|
|
398
|
+
hyperlink.append(keep)
|
|
399
|
+
return "changed"
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Word 文档语义层:标题层级(outlineLvl/样式)、表名与章节识别、表块信息汇总、同名表去重。"""
|
|
2
|
+
|
|
3
|
+
from docx.oxml.ns import qn
|
|
4
|
+
|
|
5
|
+
from .config import BOOKMARK_PREFIX, DATE_RE, EXCEL_LINK_TEXT
|
|
6
|
+
from .docx_bookmarks import bookmark_starts_in, first_bookmark_in_table
|
|
7
|
+
from .docx_grid import build_cellmap, compute_merges, header_rows, para_text, table_borders, text_grid
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def clean_paragraph_text(text: str) -> str:
|
|
11
|
+
text = text.replace(EXCEL_LINK_TEXT, "")
|
|
12
|
+
return text.strip()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def style_name_of(paragraph) -> str:
|
|
16
|
+
try:
|
|
17
|
+
return paragraph.style.name or ""
|
|
18
|
+
except Exception:
|
|
19
|
+
return ""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def is_heading_style(name: str) -> bool:
|
|
23
|
+
return ("标题" in name) or ("Heading" in name) or name.lower().startswith("heading")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def paragraph_outline_level(p_el) -> int | None:
|
|
27
|
+
"""审计软件生成的文档常直接用 w:outlineLvl 标记标题层级。"""
|
|
28
|
+
pPr = p_el.find(qn("w:pPr"))
|
|
29
|
+
if pPr is None:
|
|
30
|
+
return None
|
|
31
|
+
lvl = pPr.find(qn("w:outlineLvl"))
|
|
32
|
+
if lvl is None:
|
|
33
|
+
return None
|
|
34
|
+
value = lvl.get(qn("w:val"))
|
|
35
|
+
if value is not None and value.lstrip("-").isdigit():
|
|
36
|
+
return int(value)
|
|
37
|
+
return None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def is_heading_paragraph(p) -> bool:
|
|
41
|
+
return paragraph_outline_level(p._p) is not None or is_heading_style(style_name_of(p))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def heading_level(p) -> int | None:
|
|
45
|
+
"""标题层级;仅样式标为标题时按最低优先级处理。"""
|
|
46
|
+
level = paragraph_outline_level(p._p)
|
|
47
|
+
if level is not None:
|
|
48
|
+
return level
|
|
49
|
+
if is_heading_style(style_name_of(p)):
|
|
50
|
+
return 99
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def table_context(doc, items, table_index: int) -> tuple[object, str, str, str]:
|
|
55
|
+
"""返回 (书签锚点段落, 表名, 章节名, 日期提示)。"""
|
|
56
|
+
paragraphs_before = []
|
|
57
|
+
seen_tables = 0
|
|
58
|
+
for kind, obj in items:
|
|
59
|
+
if kind == "tbl":
|
|
60
|
+
if seen_tables == table_index:
|
|
61
|
+
break
|
|
62
|
+
seen_tables += 1
|
|
63
|
+
elif kind == "p":
|
|
64
|
+
paragraphs_before.append(obj)
|
|
65
|
+
|
|
66
|
+
name = ""
|
|
67
|
+
name_index = None
|
|
68
|
+
for idx in range(len(paragraphs_before) - 1, -1, -1):
|
|
69
|
+
text = clean_paragraph_text(para_text(paragraphs_before[idx]._p))
|
|
70
|
+
if text == "" or DATE_RE.match(text):
|
|
71
|
+
continue
|
|
72
|
+
name = text
|
|
73
|
+
name_index = idx
|
|
74
|
+
break
|
|
75
|
+
|
|
76
|
+
# 书签与“回到 Excel”链接锚定在表格前最近的非空段落(多为表格标题行或日期行),
|
|
77
|
+
# 这样每张表各有一个独占锚点,避免同一段落被多张表共用。
|
|
78
|
+
anchor = None
|
|
79
|
+
for p in reversed(paragraphs_before):
|
|
80
|
+
if clean_paragraph_text(para_text(p._p)) != "":
|
|
81
|
+
anchor = p
|
|
82
|
+
break
|
|
83
|
+
|
|
84
|
+
# 章节取“层级高于表名所在标题”的最近标题,找不到时退化为最近的标题段落。
|
|
85
|
+
name_level = paragraph_outline_level(paragraphs_before[name_index]._p) if name_index is not None else None
|
|
86
|
+
section = ""
|
|
87
|
+
start = (name_index - 1) if name_index is not None else (len(paragraphs_before) - 1)
|
|
88
|
+
nearest_heading = ""
|
|
89
|
+
for idx in range(start, -1, -1):
|
|
90
|
+
p = paragraphs_before[idx]
|
|
91
|
+
text = clean_paragraph_text(para_text(p._p))
|
|
92
|
+
if text == "" or DATE_RE.match(text):
|
|
93
|
+
continue
|
|
94
|
+
level = heading_level(p)
|
|
95
|
+
if level is None:
|
|
96
|
+
continue
|
|
97
|
+
if not nearest_heading:
|
|
98
|
+
nearest_heading = text
|
|
99
|
+
if name_level is not None and level < name_level:
|
|
100
|
+
section = text
|
|
101
|
+
break
|
|
102
|
+
if not section:
|
|
103
|
+
section = nearest_heading
|
|
104
|
+
|
|
105
|
+
date_hint = ""
|
|
106
|
+
for p in reversed(paragraphs_before):
|
|
107
|
+
text = clean_paragraph_text(para_text(p._p))
|
|
108
|
+
if DATE_RE.match(text):
|
|
109
|
+
date_hint = text
|
|
110
|
+
break
|
|
111
|
+
return anchor, name, section, date_hint
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def collect_table_info(doc, items, index: int, tab) -> dict:
|
|
115
|
+
tbl = tab._tbl
|
|
116
|
+
trs, cellmap, ncols = build_cellmap(tbl)
|
|
117
|
+
merges = compute_merges(cellmap)
|
|
118
|
+
borders = table_borders(tbl)
|
|
119
|
+
grid = text_grid(tbl, cellmap)
|
|
120
|
+
anchors, name, section, date_hint = table_context(doc, items, index)
|
|
121
|
+
code = f"T{index + 1:02d}"
|
|
122
|
+
# 锚点段落上已有的书签(导出/书签命令会据此复用;这里不做臆测命名)
|
|
123
|
+
anchor_names = bookmark_starts_in(anchors._p) if anchors is not None else []
|
|
124
|
+
own_bookmark = next((n for n in anchor_names if n.startswith(BOOKMARK_PREFIX)), "")
|
|
125
|
+
return {
|
|
126
|
+
"index": index,
|
|
127
|
+
"code": code,
|
|
128
|
+
"tbl": tbl,
|
|
129
|
+
"nrows": len(trs),
|
|
130
|
+
"ncols": ncols,
|
|
131
|
+
"cellmap": cellmap,
|
|
132
|
+
"text": grid,
|
|
133
|
+
"merges": merges,
|
|
134
|
+
"borders": borders,
|
|
135
|
+
"header_rows": header_rows(trs),
|
|
136
|
+
"bookmark": own_bookmark,
|
|
137
|
+
"old_bookmark": first_bookmark_in_table(tbl),
|
|
138
|
+
"name": name or f"表{index + 1}",
|
|
139
|
+
"section": section or "(未识别章节)",
|
|
140
|
+
"date_hint": date_hint,
|
|
141
|
+
"anchor_p": anchors._p if anchors is not None else None,
|
|
142
|
+
"anchor_paragraph": anchors,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def dedupe_table_names(infos: list[dict]) -> None:
|
|
147
|
+
"""同章节下多张表名称重复时,用日期或表号区分,便于在 Excel 中辨认。"""
|
|
148
|
+
seen: set[str] = set()
|
|
149
|
+
for info in infos:
|
|
150
|
+
name = info["name"]
|
|
151
|
+
if name in seen:
|
|
152
|
+
hint = info["date_hint"]
|
|
153
|
+
candidate = f"{name}({hint})" if hint else f"{name}(表{info['index'] + 1})"
|
|
154
|
+
if candidate in seen:
|
|
155
|
+
candidate = f"{name}(表{info['index'] + 1})"
|
|
156
|
+
info["name"] = candidate
|
|
157
|
+
seen.add(info["name"])
|