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.
- package/README.md +152 -148
- package/dsh_plugins/audit-e2w/lib/index.js +9 -1
- package/dsh_plugins/audit-e2w/lib/skill.js +3 -2
- package/excel_word/README.md +45 -37
- package/excel_word/qoder_engine.py +10 -11
- package/excel_word/wesync/__init__.py +1 -1
- 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 +1 -0
- package/excel_word/wesync/api.py +544 -92
- package/excel_word/wesync/backup_history.py +44 -9
- package/excel_word/wesync/cli.py +19 -1
- package/excel_word/wesync/commands/blank_cmd.py +59 -30
- package/excel_word/wesync/commands/bookmark_cmd.py +41 -10
- package/excel_word/wesync/commands/clear_cmd.py +5 -3
- package/excel_word/wesync/commands/export_cmd.py +270 -220
- package/excel_word/wesync/commands/incremental_cmd.py +157 -0
- package/excel_word/wesync/commands/marks_cmd.py +24 -16
- package/excel_word/wesync/commands/placeholders_cmd.py +12 -7
- package/excel_word/wesync/commands/writeback_cmd.py +72 -116
- package/excel_word/wesync/config.py +3 -2
- package/excel_word/wesync/console.py +9 -2
- package/excel_word/wesync/docx_bookmarks.py +12 -5
- package/excel_word/wesync/docx_columns.py +1 -1
- package/excel_word/wesync/docx_marks.py +183 -75
- package/excel_word/wesync/docx_placeholders.py +65 -26
- package/excel_word/wesync/excel_blocks.py +144 -185
- package/excel_word/wesync/excel_export.py +15 -8
- package/excel_word/wesync/excel_marks.py +265 -193
- package/excel_word/wesync/fsops.py +11 -6
- package/excel_word/wesync/gui/__init__.py +21 -2
- package/excel_word/wesync/gui/app.py +243 -29
- package/excel_word/wesync/gui/manual.py +5 -3
- package/excel_word/wesync/gui/pages/__init__.py +53 -4
- package/excel_word/wesync/gui/pages/_page.py +129 -7
- package/excel_word/wesync/gui/pages/ai_page.py +854 -0
- package/excel_word/wesync/gui/pages/batch_page.py +64 -31
- package/excel_word/wesync/gui/pages/blank_page.py +529 -464
- package/excel_word/wesync/gui/pages/bookmark_page.py +97 -37
- package/excel_word/wesync/gui/pages/export_page.py +140 -87
- package/excel_word/wesync/gui/pages/feedback_page.py +196 -0
- package/excel_word/wesync/gui/pages/format_page.py +551 -429
- package/excel_word/wesync/gui/pages/help_page.py +73 -7
- package/excel_word/wesync/gui/pages/history_page.py +167 -144
- package/excel_word/wesync/gui/pages/marks_page.py +210 -177
- package/excel_word/wesync/gui/pages/settings_page.py +450 -0
- package/excel_word/wesync/gui/pages/writeback_page.py +167 -146
- package/excel_word/wesync/gui/runner.py +29 -13
- package/excel_word/wesync/gui/store.py +23 -7
- package/excel_word/wesync/gui/theme.py +220 -49
- package/excel_word/wesync/gui/widgets/ai_config.py +362 -0
- package/excel_word/wesync/gui/widgets/cards.py +124 -5
- package/excel_word/wesync/gui/widgets/chips.py +565 -0
- package/excel_word/wesync/gui/widgets/filebar.py +1 -1
- package/excel_word/wesync/gui/widgets/logpane.py +24 -8
- package/excel_word/wesync/gui/widgets/nav.py +267 -12
- package/excel_word/wesync/gui/widgets/theme_choice.py +93 -0
- package/excel_word/wesync/report_format.py +9 -1
- package/excel_word/wesync/settings.py +35 -0
- package/excel_word/wesync/sort_rules.py +75 -26
- package/excel_word/wesync/valuefmt.py +40 -17
- package/package.json +53 -51
- 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 -156
- package/qoder_plugins/audit-toolbox/catalog.mjs +13 -13
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
"""
|
|
2
|
-
|
|
3
|
-
约定:
|
|
4
|
-
* 高亮(``w:highlight``,任意颜色)视为“待处理位置”的输入标记;
|
|
5
|
-
* 每个标记(同一段落内连续的高亮 run 合并为一个)用一个不可见书签 ``MRK_0001`` 定位;
|
|
6
|
-
* 书签只做锚点,不修改任何文字;同步写回时才按 Excel 内容替换文字并去掉高亮。
|
|
7
|
-
"""
|
|
1
|
+
"""高亮标记层:识别显式高亮并建立可定位、可写回的书签锚点。"""
|
|
8
2
|
|
|
9
3
|
from __future__ import annotations
|
|
10
4
|
|
|
@@ -16,6 +10,7 @@ from docx.oxml.ns import qn
|
|
|
16
10
|
from docx.opc.constants import RELATIONSHIP_TYPE as RT
|
|
17
11
|
|
|
18
12
|
from .docx_bookmarks import bookmark_names, next_bookmark_id
|
|
13
|
+
from .errors import SyncError
|
|
19
14
|
|
|
20
15
|
MARK_PREFIX = "MRK_"
|
|
21
16
|
MARK_NAME_RE = re.compile(rf"^{MARK_PREFIX}\d{{4}}$")
|
|
@@ -23,16 +18,16 @@ MARK_NAME_RE = re.compile(rf"^{MARK_PREFIX}\d{{4}}$")
|
|
|
23
18
|
|
|
24
19
|
@dataclass
|
|
25
20
|
class Mark:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
|
36
31
|
|
|
37
32
|
|
|
38
33
|
def _highlight_val(r_el) -> str | None:
|
|
@@ -45,32 +40,86 @@ def _highlight_val(r_el) -> str | None:
|
|
|
45
40
|
return hl.get(qn("w:val"))
|
|
46
41
|
|
|
47
42
|
|
|
48
|
-
def
|
|
49
|
-
|
|
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
|
|
50
67
|
|
|
51
68
|
|
|
52
|
-
def
|
|
53
|
-
|
|
54
|
-
|
|
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")
|
|
55
76
|
|
|
56
77
|
|
|
57
|
-
def
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
|
66
98
|
|
|
67
99
|
|
|
68
100
|
def _run_texts(runs: list) -> str:
|
|
69
101
|
return "".join("".join(t.text or "" for t in r.iter(qn("w:t"))) for r in runs)
|
|
70
102
|
|
|
71
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
|
+
|
|
72
122
|
def _bookmark_ranges_in_paragraph(p_el, prefix: str = "") -> list[tuple[str, int, int]]:
|
|
73
|
-
"""返回段落内的书签范围 ``[(名字, 起始子元素下标, 结束子元素下标)]``。"""
|
|
74
123
|
children = list(p_el.iterchildren())
|
|
75
124
|
starts: dict[str, tuple[str, int]] = {}
|
|
76
125
|
ranges: list[tuple[str, int, int]] = []
|
|
@@ -87,43 +136,74 @@ def _bookmark_ranges_in_paragraph(p_el, prefix: str = "") -> list[tuple[str, int
|
|
|
87
136
|
return ranges
|
|
88
137
|
|
|
89
138
|
|
|
90
|
-
def
|
|
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
|
|
91
166
|
children = list(p_el.iterchildren())
|
|
92
167
|
existing = {start: name for name, start, _end in _bookmark_ranges_in_paragraph(p_el, MARK_PREFIX)}
|
|
93
168
|
paragraph_text = "".join(t.text or "" for t in p_el.iter(qn("w:t"))).strip()
|
|
94
169
|
|
|
95
170
|
group: list = []
|
|
96
171
|
group_start = 0
|
|
172
|
+
paragraph_mark = _paragraph_mark_highlighted(p_el)
|
|
97
173
|
for index, child in enumerate(children):
|
|
98
|
-
payload = _payload_of_child(child)
|
|
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")))
|
|
99
177
|
if payload:
|
|
100
178
|
if not group:
|
|
101
179
|
group_start = index
|
|
102
180
|
group.append((child, payload))
|
|
103
181
|
continue
|
|
104
|
-
if group:
|
|
105
|
-
_emit_mark(p_el, children, group, group_start, index - 1, location, paragraph_text, existing, marks)
|
|
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)
|
|
106
184
|
group = []
|
|
107
185
|
if group:
|
|
108
|
-
_emit_mark(p_el, children, group, group_start, len(children) - 1, location, paragraph_text, existing, marks)
|
|
186
|
+
_emit_mark(p_el, part, doc, children, group, group_start, len(children) - 1, location, paragraph_text, existing, marks)
|
|
109
187
|
|
|
110
188
|
|
|
111
|
-
def _emit_mark(p_el, children, group, start_index, end_index, location, paragraph_text, existing, marks) -> None:
|
|
189
|
+
def _emit_mark(p_el, part, doc, children, group, start_index, end_index, location, paragraph_text, existing, marks) -> None:
|
|
112
190
|
runs: list = []
|
|
113
191
|
for _child, payload in group:
|
|
114
192
|
runs.extend(payload)
|
|
115
|
-
|
|
193
|
+
text = _run_texts(runs)
|
|
194
|
+
if not text.strip():
|
|
116
195
|
return
|
|
117
196
|
mark = Mark(
|
|
118
197
|
index=len(marks) + 1,
|
|
119
198
|
p_el=p_el,
|
|
120
199
|
children=[child for child, _payload in group],
|
|
121
200
|
runs=runs,
|
|
122
|
-
text=
|
|
201
|
+
text=text,
|
|
123
202
|
context=paragraph_text[:80],
|
|
124
203
|
location=location,
|
|
204
|
+
part=part,
|
|
205
|
+
doc=doc,
|
|
125
206
|
)
|
|
126
|
-
# 复用已经存在的 MRK_ 书签(重复导出时保持稳定)
|
|
127
207
|
name = existing.get(start_index - 1)
|
|
128
208
|
if name:
|
|
129
209
|
mark.bookmark = name
|
|
@@ -131,46 +211,66 @@ def _emit_mark(p_el, children, group, start_index, end_index, location, paragrap
|
|
|
131
211
|
|
|
132
212
|
|
|
133
213
|
def find_marks(doc) -> list[Mark]:
|
|
134
|
-
"""
|
|
214
|
+
"""按文档顺序找出正文、页眉页脚里的高亮;表格内高亮不参与。"""
|
|
215
|
+
_duplicate_mark_bookmarks(doc)
|
|
135
216
|
marks: list[Mark] = []
|
|
136
|
-
|
|
137
|
-
|
|
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)
|
|
138
228
|
return marks
|
|
139
229
|
|
|
140
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
|
+
|
|
141
242
|
def iter_paragraph_locations(doc):
|
|
142
|
-
"""
|
|
243
|
+
"""按文档顺序产出正文与页眉页脚段落,表格内段落不产出。"""
|
|
143
244
|
para_no = 0
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
yield p_el, f"表{table_no}"
|
|
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
|
|
154
254
|
|
|
155
255
|
|
|
156
256
|
def processed_marks(doc, pending: set[str]) -> list[dict]:
|
|
157
|
-
"""
|
|
257
|
+
"""找出仍有 MRK 书签但已不高亮的标记。"""
|
|
158
258
|
result: list[dict] = []
|
|
159
259
|
for p_el, location in iter_paragraph_locations(doc):
|
|
160
260
|
for name, start, end in _bookmark_ranges_in_paragraph(p_el, MARK_PREFIX):
|
|
161
261
|
if not name or name in pending:
|
|
162
262
|
continue
|
|
163
|
-
children = list(p_el.iterchildren())[start + 1
|
|
164
|
-
text = "".join(
|
|
165
|
-
"".join(t.text or "" for t in child.iter(qn("w:t"))) for child in children
|
|
166
|
-
)
|
|
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)
|
|
167
265
|
result.append({"bookmark": name, "text": text.strip(), "location": location})
|
|
168
266
|
return result
|
|
169
267
|
|
|
170
268
|
|
|
171
269
|
def ensure_mark_bookmarks(doc, marks: list[Mark]) -> int:
|
|
172
|
-
|
|
173
|
-
existing =
|
|
270
|
+
_duplicate_mark_bookmarks(doc)
|
|
271
|
+
existing = set()
|
|
272
|
+
for root, _label in _containers(doc):
|
|
273
|
+
existing |= bookmark_names(root)
|
|
174
274
|
numbers = [int(name[len(MARK_PREFIX):]) for name in existing if MARK_NAME_RE.match(name)]
|
|
175
275
|
next_no = (max(numbers) + 1) if numbers else 1
|
|
176
276
|
bookmark_id = next_bookmark_id(doc)
|
|
@@ -200,12 +300,12 @@ def ensure_mark_bookmarks(doc, marks: list[Mark]) -> int:
|
|
|
200
300
|
|
|
201
301
|
|
|
202
302
|
def mark_payload_by_bookmark(doc, name: str):
|
|
203
|
-
|
|
204
|
-
for p_el in doc
|
|
303
|
+
_duplicate_mark_bookmarks(doc)
|
|
304
|
+
for p_el, _location in iter_paragraph_locations(doc):
|
|
205
305
|
for range_name, start, end in _bookmark_ranges_in_paragraph(p_el):
|
|
206
306
|
if range_name != name:
|
|
207
307
|
continue
|
|
208
|
-
children = list(p_el.iterchildren())[start + 1
|
|
308
|
+
children = list(p_el.iterchildren())[start + 1:end]
|
|
209
309
|
runs: list = []
|
|
210
310
|
for child in children:
|
|
211
311
|
if child.tag == qn("w:r"):
|
|
@@ -225,14 +325,21 @@ def _remove_highlight(r_el) -> None:
|
|
|
225
325
|
rPr.remove(hl)
|
|
226
326
|
|
|
227
327
|
|
|
228
|
-
def
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
+
|
|
232
339
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
"""
|
|
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。"""
|
|
236
343
|
if not runs:
|
|
237
344
|
return "deleted"
|
|
238
345
|
current_text = _run_texts(runs)
|
|
@@ -240,7 +347,6 @@ def apply_mark_change(
|
|
|
240
347
|
if new_text == "" and link == "":
|
|
241
348
|
return "unchanged"
|
|
242
349
|
|
|
243
|
-
# 先看当前是否已经是超链接、指向哪里(用于幂等判断)
|
|
244
350
|
existing_link = ""
|
|
245
351
|
for child in children:
|
|
246
352
|
if child.tag == qn("w:hyperlink"):
|
|
@@ -249,12 +355,12 @@ def apply_mark_change(
|
|
|
249
355
|
existing_link = rel.target_ref if rel is not None else ""
|
|
250
356
|
break
|
|
251
357
|
|
|
252
|
-
|
|
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)
|
|
253
360
|
changed = current_text != target_text or (link or "") != (existing_link or "") or highlight_pending
|
|
254
361
|
if not changed:
|
|
255
362
|
return "unchanged"
|
|
256
363
|
|
|
257
|
-
# 解开已有超链接,改由第一个 run 承载文字
|
|
258
364
|
for child in list(children):
|
|
259
365
|
if child.tag == qn("w:hyperlink"):
|
|
260
366
|
for r in list(child.iter(qn("w:r"))):
|
|
@@ -277,6 +383,8 @@ def apply_mark_change(
|
|
|
277
383
|
keep.append(node)
|
|
278
384
|
if not keep_highlight:
|
|
279
385
|
_remove_highlight(keep)
|
|
386
|
+
if style_highlight:
|
|
387
|
+
_add_no_highlight(keep)
|
|
280
388
|
for extra in runs[1:]:
|
|
281
389
|
parent = extra.getparent()
|
|
282
390
|
if parent is not None:
|
|
@@ -12,22 +12,21 @@ PLACEHOLDER_RE = re.compile(r"\{([^{}\r\n]{1,40})\}")
|
|
|
12
12
|
VALUES_SHEET = "正文信息"
|
|
13
13
|
|
|
14
14
|
|
|
15
|
+
class PlaceholderValues(dict[str, str]):
|
|
16
|
+
def __init__(self, filled_fields: set[str]):
|
|
17
|
+
super().__init__()
|
|
18
|
+
self.filled_fields = filled_fields
|
|
19
|
+
|
|
20
|
+
|
|
15
21
|
def _iter_paragraphs(doc):
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
yield from container.paragraphs
|
|
19
|
-
for table in container.tables:
|
|
20
|
-
for row in table.rows:
|
|
21
|
-
for cell in row.cells:
|
|
22
|
-
yield from _walk(cell)
|
|
23
|
-
|
|
24
|
-
yield from _walk(doc)
|
|
22
|
+
"""正文与页眉页脚的直接段落;表格内占位符不参与。"""
|
|
23
|
+
yield from doc.paragraphs
|
|
25
24
|
for section in doc.sections:
|
|
26
25
|
for part in (section.header, section.footer,
|
|
27
26
|
section.first_page_header, section.first_page_footer,
|
|
28
27
|
section.even_page_header, section.even_page_footer):
|
|
29
28
|
if part is not None:
|
|
30
|
-
yield from
|
|
29
|
+
yield from part.paragraphs
|
|
31
30
|
|
|
32
31
|
|
|
33
32
|
def scan_placeholders(doc) -> list[str]:
|
|
@@ -74,7 +73,12 @@ def _replace_in_paragraph(p, values: dict[str, str]) -> list[tuple[str, str]]:
|
|
|
74
73
|
def apply_placeholders(doc, values: dict[str, str], *, dry_run: bool = False) -> dict:
|
|
75
74
|
"""返回 {"replaced": [(字段, 新值)], "missing": [字段]};dry_run 只列计划不改文档。"""
|
|
76
75
|
fields = scan_placeholders(doc)
|
|
77
|
-
|
|
76
|
+
filled = getattr(
|
|
77
|
+
values,
|
|
78
|
+
"filled_fields",
|
|
79
|
+
{key for key, value in values.items() if str(value).strip()},
|
|
80
|
+
)
|
|
81
|
+
missing = [f for f in fields if f not in filled]
|
|
78
82
|
if dry_run:
|
|
79
83
|
return {"replaced": [(f, str(values[f])) for f in fields if f not in missing], "missing": missing}
|
|
80
84
|
replaced = []
|
|
@@ -84,7 +88,10 @@ def apply_placeholders(doc, values: dict[str, str], *, dry_run: bool = False) ->
|
|
|
84
88
|
return {"replaced": replaced, "missing": missing}
|
|
85
89
|
|
|
86
90
|
|
|
87
|
-
def read_placeholder_values(
|
|
91
|
+
def read_placeholder_values(
|
|
92
|
+
xlsx_path, *, resolve_formulas: bool = True, zero_mode: str = "excel",
|
|
93
|
+
wb=None, cached_wb=None,
|
|
94
|
+
) -> PlaceholderValues:
|
|
88
95
|
"""读工作簿「正文信息」表(A=字段 B=值,首行表头);无该表返回空。
|
|
89
96
|
|
|
90
97
|
值按单元格在 Excel 里的显示效果转换(与表格回写同口径):日期用其数字格式渲染
|
|
@@ -96,15 +103,29 @@ def read_placeholder_values(xlsx_path, *, resolve_formulas: bool = True) -> dict
|
|
|
96
103
|
from .errors import SyncError
|
|
97
104
|
from .valuefmt import display_cell, formula_result
|
|
98
105
|
|
|
99
|
-
|
|
106
|
+
own_wb = wb is None
|
|
107
|
+
if own_wb:
|
|
108
|
+
wb = load_workbook(str(xlsx_path))
|
|
100
109
|
if VALUES_SHEET not in wb.sheetnames:
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
if own_wb:
|
|
111
|
+
wb.close()
|
|
112
|
+
return PlaceholderValues(set())
|
|
113
|
+
if resolve_formulas and cached_wb is None:
|
|
114
|
+
cached_wb = load_workbook(str(xlsx_path), data_only=True)
|
|
103
115
|
ws = wb[VALUES_SHEET]
|
|
104
116
|
cached_ws = cached_wb[VALUES_SHEET] if cached_wb is not None else None
|
|
105
|
-
values =
|
|
117
|
+
values = PlaceholderValues(set())
|
|
118
|
+
seen_rows: dict[str, int] = {}
|
|
106
119
|
for row in range(2, (ws.max_row or 1) + 1):
|
|
107
120
|
field = str(ws.cell(row, 1).value or "").strip()
|
|
121
|
+
if not field:
|
|
122
|
+
continue
|
|
123
|
+
if field in seen_rows:
|
|
124
|
+
raise SyncError(
|
|
125
|
+
f"「{VALUES_SHEET}」字段“{field}”重复出现在第 {seen_rows[field]} 行和第 {row} 行,"
|
|
126
|
+
"无法确定使用哪个值。请合并为一行后再同步。"
|
|
127
|
+
)
|
|
128
|
+
seen_rows[field] = row
|
|
108
129
|
cell = ws.cell(row, 2)
|
|
109
130
|
raw = cell.value
|
|
110
131
|
if resolve_formulas and isinstance(raw, str) and raw.startswith("="):
|
|
@@ -118,7 +139,9 @@ def read_placeholder_values(xlsx_path, *, resolve_formulas: bool = True) -> dict
|
|
|
118
139
|
if not resolve_formulas and isinstance(raw, str) and raw.startswith("="):
|
|
119
140
|
values[field] = raw
|
|
120
141
|
else:
|
|
121
|
-
values[field] = display_cell(cell, raw)
|
|
142
|
+
values[field] = display_cell(cell, raw, zero_mode=zero_mode)
|
|
143
|
+
if raw is not None and not (isinstance(raw, str) and not raw.strip()):
|
|
144
|
+
values.filled_fields.add(field)
|
|
122
145
|
return values
|
|
123
146
|
|
|
124
147
|
|
|
@@ -131,30 +154,46 @@ def init_placeholder_sheet(wb):
|
|
|
131
154
|
ws = wb.create_sheet(VALUES_SHEET)
|
|
132
155
|
ws.cell(1, 1, "字段")
|
|
133
156
|
ws.cell(1, 2, "值")
|
|
134
|
-
|
|
157
|
+
ws.cell(1, 3, "来源")
|
|
158
|
+
for c in (1, 2, 3):
|
|
135
159
|
ws.cell(1, c).font = Font(name=FONT_NAME, size=FONT_SIZE, bold=True)
|
|
136
160
|
ws.column_dimensions["A"].width = 24
|
|
137
161
|
ws.column_dimensions["B"].width = 48
|
|
162
|
+
ws.column_dimensions["C"].width = 10
|
|
138
163
|
return ws
|
|
139
164
|
|
|
140
165
|
|
|
141
|
-
def
|
|
142
|
-
"""
|
|
143
|
-
from .
|
|
166
|
+
def append_placeholder_sheet(wb, fields: list[str], *, source: str = "全量") -> list[str]:
|
|
167
|
+
"""在工作簿末尾追加缺失字段,不改既有行。"""
|
|
168
|
+
from openpyxl.styles import Font
|
|
169
|
+
from .config import FONT_NAME, FONT_SIZE
|
|
144
170
|
|
|
145
|
-
check_file_free(xlsx_path, False)
|
|
146
|
-
wb = load_workbook_for_save(xlsx_path)
|
|
147
171
|
created = VALUES_SHEET not in wb.sheetnames
|
|
148
172
|
ws = init_placeholder_sheet(wb) if created else wb[VALUES_SHEET]
|
|
173
|
+
if not created and not ws.cell(1, 3).value:
|
|
174
|
+
ws.cell(1, 3, "来源")
|
|
175
|
+
ws.cell(1, 3).font = Font(name=FONT_NAME, size=FONT_SIZE, bold=True)
|
|
176
|
+
ws.column_dimensions["C"].width = 10
|
|
149
177
|
existing = {str(ws.cell(r, 1).value or "").strip() for r in range(2, (ws.max_row or 1) + 1)}
|
|
150
178
|
added = []
|
|
151
179
|
for field in fields:
|
|
152
180
|
if field not in existing:
|
|
153
|
-
ws.append([field, ""])
|
|
181
|
+
ws.append([field, "", source])
|
|
182
|
+
existing.add(field)
|
|
154
183
|
added.append(field)
|
|
184
|
+
return added
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def write_placeholder_sheet(xlsx_path, fields: list[str], *, source: str = "全量", backup: bool = True) -> list[str]:
|
|
188
|
+
"""给工作簿生成/补齐「正文信息」填写骨架,保留已填值;返回新增字段。"""
|
|
189
|
+
from .fsops import check_file_free, load_workbook_for_save, save_docx_xlsx_transaction
|
|
190
|
+
|
|
191
|
+
check_file_free(xlsx_path, False)
|
|
192
|
+
wb = load_workbook_for_save(xlsx_path)
|
|
193
|
+
created = VALUES_SHEET not in wb.sheetnames
|
|
194
|
+
added = append_placeholder_sheet(wb, fields, source=source)
|
|
155
195
|
if added or created:
|
|
156
|
-
|
|
157
|
-
wb.save(str(xlsx_path))
|
|
196
|
+
save_docx_xlsx_transaction(xlsx_path=xlsx_path, wb=wb, backup=backup)
|
|
158
197
|
wb.close()
|
|
159
198
|
if getattr(wb, "vba_archive", None):
|
|
160
199
|
wb.vba_archive.close()
|