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,156 @@
|
|
|
1
|
+
"""空白表识别与删除:整表全空 / 仅表头有字的表格列为候选,确认后可连表标题一起删除。
|
|
2
|
+
|
|
3
|
+
判定口径(保守,宁可漏报不误报):
|
|
4
|
+
- 首列(多为行次/项目名称)不参与判断;单列表除外,首列即全部内容;
|
|
5
|
+
- 整表全空:除首列外所有单元格都没有文字(空白字符不算文字);
|
|
6
|
+
- 仅表头有字:有文字的行全部是表头行(w:tblHeader 标记,未标记时按首行),
|
|
7
|
+
且表头之外至少还有一行数据区、数据区全空。
|
|
8
|
+
数据区只有 —、0 等占位内容的表**不算**空白。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import re
|
|
14
|
+
|
|
15
|
+
from docx.oxml.ns import qn
|
|
16
|
+
|
|
17
|
+
from .config import DATE_RE
|
|
18
|
+
from .docx_bookmarks import is_tool_bookmark
|
|
19
|
+
from .docx_grid import body_items, build_cellmap, header_rows, para_text, text_grid
|
|
20
|
+
from .docx_meta import clean_paragraph_text
|
|
21
|
+
|
|
22
|
+
# 章节大标题(一、/第X节 等)不是表标题,删表时不应连带删除
|
|
23
|
+
SECTION_RE = re.compile(r"^(第[一二三四五六七八九十百\d]+[节章部分]|[一二三四五六七八九十]+、)")
|
|
24
|
+
|
|
25
|
+
CAPTION_MAX_LEN = 60
|
|
26
|
+
SENTENCE_ENDS = "。;!?;!?"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _looks_like_caption(text: str) -> bool:
|
|
30
|
+
if not text or len(text) > CAPTION_MAX_LEN:
|
|
31
|
+
return False
|
|
32
|
+
if SECTION_RE.match(text):
|
|
33
|
+
return False
|
|
34
|
+
return text[-1] not in SENTENCE_ENDS
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _caption_before(items: list, pos: int):
|
|
38
|
+
"""从表格位置向前找表标题:跳过空段与日期行,遇到正文句/另一张表即止。
|
|
39
|
+
|
|
40
|
+
返回 (标题段落元素或 None, 标题文字, 标题与表格之间的空段/日期段列表)。
|
|
41
|
+
"""
|
|
42
|
+
extras: list = []
|
|
43
|
+
idx = pos - 1
|
|
44
|
+
while idx >= 0:
|
|
45
|
+
kind, obj = items[idx]
|
|
46
|
+
if kind != "p":
|
|
47
|
+
break
|
|
48
|
+
text = clean_paragraph_text(para_text(obj._p))
|
|
49
|
+
if text == "" or DATE_RE.match(text):
|
|
50
|
+
extras.append(obj._p)
|
|
51
|
+
idx -= 1
|
|
52
|
+
continue
|
|
53
|
+
if _looks_like_caption(text):
|
|
54
|
+
return obj._p, text, extras
|
|
55
|
+
break
|
|
56
|
+
return None, "", []
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _blank_kind(trs, grid) -> str | None:
|
|
60
|
+
nrows = len(trs)
|
|
61
|
+
ncols = max((len(row) for row in grid), default=0)
|
|
62
|
+
# 首列多为行次/项目名称,不参与空白判断;单列表除外(首列即全部内容)
|
|
63
|
+
start_col = 1 if ncols >= 2 else 0
|
|
64
|
+
filled = [
|
|
65
|
+
(r, c)
|
|
66
|
+
for r, row in enumerate(grid)
|
|
67
|
+
for c, text in enumerate(row)
|
|
68
|
+
if c >= start_col and text is not None and text.strip()
|
|
69
|
+
]
|
|
70
|
+
if not filled:
|
|
71
|
+
return "整表全空"
|
|
72
|
+
heads = header_rows(trs) or {0}
|
|
73
|
+
if nrows > max(heads) + 1 and all(r in heads for r, _ in filled):
|
|
74
|
+
return "仅表头有字"
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def find_blank_tables(doc) -> list[dict]:
|
|
79
|
+
"""扫描全文,返回空白表候选清单(只读,不改文档)。"""
|
|
80
|
+
items = body_items(doc)
|
|
81
|
+
candidates: list[dict] = []
|
|
82
|
+
table_index = -1
|
|
83
|
+
for pos, (kind, obj) in enumerate(items):
|
|
84
|
+
if kind != "tbl":
|
|
85
|
+
continue
|
|
86
|
+
table_index += 1
|
|
87
|
+
tbl = obj._tbl
|
|
88
|
+
trs, cellmap, ncols = build_cellmap(tbl)
|
|
89
|
+
grid = text_grid(tbl, cellmap)
|
|
90
|
+
blank_kind = _blank_kind(trs, grid)
|
|
91
|
+
if blank_kind is None:
|
|
92
|
+
continue
|
|
93
|
+
caption_p, caption_text, extra_ps = _caption_before(items, pos)
|
|
94
|
+
candidates.append(
|
|
95
|
+
{
|
|
96
|
+
"no": len(candidates) + 1,
|
|
97
|
+
"index": table_index,
|
|
98
|
+
"code": f"T{table_index + 1:02d}",
|
|
99
|
+
"kind": blank_kind,
|
|
100
|
+
"nrows": len(trs),
|
|
101
|
+
"ncols": ncols,
|
|
102
|
+
"tbl": tbl,
|
|
103
|
+
"caption_p": caption_p,
|
|
104
|
+
"caption_text": caption_text,
|
|
105
|
+
"extra_ps": extra_ps,
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
return candidates
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _tool_bookmark_ids(doc) -> set[str]:
|
|
112
|
+
ids = set()
|
|
113
|
+
for el in doc.element.body.iter(qn("w:bookmarkStart")):
|
|
114
|
+
name = el.get(qn("w:name")) or ""
|
|
115
|
+
if is_tool_bookmark(name):
|
|
116
|
+
ids.add(el.get(qn("w:id")))
|
|
117
|
+
return ids
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _cleanup_orphan_bookmarks(doc, tool_ids: set[str]) -> int:
|
|
121
|
+
"""删表/删标题后,清掉失去配对的本工具书签端点(Word 自带书签不动)。"""
|
|
122
|
+
body = doc.element.body
|
|
123
|
+
start_ids = {el.get(qn("w:id")) for el in body.iter(qn("w:bookmarkStart"))}
|
|
124
|
+
end_ids = {el.get(qn("w:id")) for el in body.iter(qn("w:bookmarkEnd"))}
|
|
125
|
+
removed = 0
|
|
126
|
+
for el in list(body.iter(qn("w:bookmarkStart"))):
|
|
127
|
+
if el.get(qn("w:id")) in tool_ids and el.get(qn("w:id")) not in end_ids:
|
|
128
|
+
el.getparent().remove(el)
|
|
129
|
+
removed += 1
|
|
130
|
+
for el in list(body.iter(qn("w:bookmarkEnd"))):
|
|
131
|
+
if el.get(qn("w:id")) in tool_ids and el.get(qn("w:id")) not in start_ids:
|
|
132
|
+
el.getparent().remove(el)
|
|
133
|
+
removed += 1
|
|
134
|
+
return removed
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def delete_blank_tables(doc, selected: list[tuple[dict, bool]]) -> dict:
|
|
138
|
+
"""按选择删除空白表。selected 为 (候选, 是否连表标题一起删) 列表。
|
|
139
|
+
|
|
140
|
+
返回 {"tables": 删表数, "captions": 删标题数, "bookmarks": 清理书签端点数}。
|
|
141
|
+
"""
|
|
142
|
+
tool_ids = _tool_bookmark_ids(doc)
|
|
143
|
+
removed_tables = 0
|
|
144
|
+
removed_captions = 0
|
|
145
|
+
for cand, with_caption in selected:
|
|
146
|
+
tbl = cand["tbl"]
|
|
147
|
+
if tbl.getparent() is not None:
|
|
148
|
+
tbl.getparent().remove(tbl)
|
|
149
|
+
removed_tables += 1
|
|
150
|
+
if with_caption and cand["caption_p"] is not None:
|
|
151
|
+
for p_el in [cand["caption_p"], *cand["extra_ps"]]:
|
|
152
|
+
if p_el.getparent() is not None:
|
|
153
|
+
p_el.getparent().remove(p_el)
|
|
154
|
+
removed_captions += 1
|
|
155
|
+
removed_bookmarks = _cleanup_orphan_bookmarks(doc, tool_ids)
|
|
156
|
+
return {"tables": removed_tables, "captions": removed_captions, "bookmarks": removed_bookmarks}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"""Word 书签层:书签读写、Table_ 随机命名与旧书签迁移、遗留〔→Excel〕文字与超链接域清理。"""
|
|
2
|
+
|
|
3
|
+
import secrets
|
|
4
|
+
import string
|
|
5
|
+
from docx.oxml import OxmlElement
|
|
6
|
+
from docx.oxml.ns import qn
|
|
7
|
+
from .config import BOOKMARK_PREFIX, EXCEL_LINK_TEXT, LEGACY_BOOKMARK_RE
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def bookmark_names(root) -> set[str]:
|
|
11
|
+
names = set()
|
|
12
|
+
for el in root.iter(qn("w:bookmarkStart")):
|
|
13
|
+
value = el.get(qn("w:name"))
|
|
14
|
+
if value:
|
|
15
|
+
names.add(value)
|
|
16
|
+
return names
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def used_bookmark_ids(doc) -> set[int]:
|
|
20
|
+
ids = set()
|
|
21
|
+
roots = [doc.element.body]
|
|
22
|
+
for section in doc.sections:
|
|
23
|
+
roots.extend(part._element for part in (
|
|
24
|
+
section.header, section.footer, section.first_page_header, section.first_page_footer,
|
|
25
|
+
section.even_page_header, section.even_page_footer,
|
|
26
|
+
) if part is not None)
|
|
27
|
+
for root in roots:
|
|
28
|
+
for el in root.iter():
|
|
29
|
+
if el.tag in (qn("w:bookmarkStart"), qn("w:bookmarkEnd")):
|
|
30
|
+
value = el.get(qn("w:id"))
|
|
31
|
+
if value and value.lstrip("-").isdigit():
|
|
32
|
+
ids.add(int(value))
|
|
33
|
+
return ids
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def first_bookmark_in_table(tbl) -> str | None:
|
|
37
|
+
for el in tbl.iter(qn("w:bookmarkStart")):
|
|
38
|
+
name = el.get(qn("w:name"))
|
|
39
|
+
if name:
|
|
40
|
+
return name
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def pair_anchor_bookmarks_with_tables(doc, candidates: set[str]) -> dict[str, int]:
|
|
45
|
+
"""把“锚点段落里的书签”与“紧随其后的表格”配对。
|
|
46
|
+
|
|
47
|
+
只认 ``candidates`` 里的书签名(通常是工作簿 C 列记录的那些);段落里其它书签
|
|
48
|
+
(例如 ``_Toc…``)忽略、不打断配对。返回 ``{书签名: 表格序号}``(序号即 doc.tables 顺序):
|
|
49
|
+
没出现在结果里的候选书签,即“对应的表格已从 Word 中删除”。
|
|
50
|
+
"""
|
|
51
|
+
if not candidates:
|
|
52
|
+
return {}
|
|
53
|
+
pairing: dict[str, int] = {}
|
|
54
|
+
pending: str | None = None
|
|
55
|
+
table_index = -1
|
|
56
|
+
for child in doc.element.body.iterchildren():
|
|
57
|
+
tag = child.tag.split("}")[1]
|
|
58
|
+
if tag == "p":
|
|
59
|
+
hits = [
|
|
60
|
+
name
|
|
61
|
+
for name in (
|
|
62
|
+
el.get(qn("w:name")) for el in child.iter(qn("w:bookmarkStart"))
|
|
63
|
+
)
|
|
64
|
+
if name in candidates
|
|
65
|
+
]
|
|
66
|
+
if hits:
|
|
67
|
+
pending = hits[-1] # 同一段落有多个候选书签时,取最后插入的那个
|
|
68
|
+
elif tag == "tbl":
|
|
69
|
+
table_index += 1
|
|
70
|
+
if pending is not None:
|
|
71
|
+
pairing[pending] = table_index
|
|
72
|
+
pending = None
|
|
73
|
+
return pairing
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def unclaimed_table_indices(doc, pairing: dict[str, int]) -> list[int]:
|
|
77
|
+
"""返回没有匹配到任何书签的 Word 表格序号列表。"""
|
|
78
|
+
claimed = set(pairing.values())
|
|
79
|
+
return [index for index in range(len(doc.tables)) if index not in claimed]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def table_index_map_by_bookmark(
|
|
83
|
+
doc, candidates: set[str], *, include_anchors: bool = False
|
|
84
|
+
) -> dict[str, int]:
|
|
85
|
+
"""书签名 → 表格序号;可兼容表内书签与旧式标题段落书签。"""
|
|
86
|
+
if not candidates:
|
|
87
|
+
return {}
|
|
88
|
+
result: dict[str, int] = {}
|
|
89
|
+
for index, tab in enumerate(doc.tables):
|
|
90
|
+
for el in tab._tbl.iter(qn("w:bookmarkStart")):
|
|
91
|
+
name = el.get(qn("w:name"))
|
|
92
|
+
if name in candidates:
|
|
93
|
+
result.setdefault(name, index)
|
|
94
|
+
if include_anchors:
|
|
95
|
+
for name, index in pair_anchor_bookmarks_with_tables(doc, candidates).items():
|
|
96
|
+
result.setdefault(name, index)
|
|
97
|
+
return result
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _ensure_cell_paragraph(tc, last: bool):
|
|
101
|
+
"""取单元格里的第一个/最后一个段落,没有就补一个。"""
|
|
102
|
+
paragraphs = tc.findall(qn("w:p"))
|
|
103
|
+
if paragraphs:
|
|
104
|
+
return paragraphs[-1] if last else paragraphs[0]
|
|
105
|
+
paragraph = OxmlElement("w:p")
|
|
106
|
+
tc.append(paragraph)
|
|
107
|
+
return paragraph
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def insert_table_bookmark(tbl, name: str, bookmark_id: int) -> bool:
|
|
111
|
+
"""把书签**整表锚定**:起点在首行首格首段开头,终点在末行末格末段末尾。
|
|
112
|
+
|
|
113
|
+
这样从 Excel 点“→定位Word表”会直接选中整张表格,而不是只停在标题行。
|
|
114
|
+
"""
|
|
115
|
+
rows = tbl.findall(qn("w:tr"))
|
|
116
|
+
if not rows:
|
|
117
|
+
return False
|
|
118
|
+
first_tc = rows[0].find(qn("w:tc"))
|
|
119
|
+
last_row_cells = rows[-1].findall(qn("w:tc"))
|
|
120
|
+
if first_tc is None or not last_row_cells:
|
|
121
|
+
return False
|
|
122
|
+
last_tc = last_row_cells[-1]
|
|
123
|
+
|
|
124
|
+
start_paragraph = _ensure_cell_paragraph(first_tc, last=False)
|
|
125
|
+
end_paragraph = _ensure_cell_paragraph(last_tc, last=True)
|
|
126
|
+
start = OxmlElement("w:bookmarkStart")
|
|
127
|
+
start.set(qn("w:id"), str(bookmark_id))
|
|
128
|
+
start.set(qn("w:name"), name)
|
|
129
|
+
end = OxmlElement("w:bookmarkEnd")
|
|
130
|
+
end.set(qn("w:id"), str(bookmark_id))
|
|
131
|
+
pPr = start_paragraph.find(qn("w:pPr"))
|
|
132
|
+
if pPr is not None:
|
|
133
|
+
pPr.addnext(start)
|
|
134
|
+
else:
|
|
135
|
+
start_paragraph.insert(0, start)
|
|
136
|
+
end_paragraph.append(end)
|
|
137
|
+
return True
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def next_bookmark_id(doc) -> int:
|
|
141
|
+
ids = used_bookmark_ids(doc)
|
|
142
|
+
return (max(ids) + 1) if ids else 1
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def new_cpah_name(existing: set[str]) -> str:
|
|
146
|
+
"""生成 Table_ + 4 位大写字母/数字随机书签名(如 Table_3K9Q)。"""
|
|
147
|
+
alphabet = string.ascii_uppercase + string.digits
|
|
148
|
+
while True:
|
|
149
|
+
name = BOOKMARK_PREFIX + "".join(secrets.choice(alphabet) for _ in range(4))
|
|
150
|
+
if name not in existing:
|
|
151
|
+
existing.add(name)
|
|
152
|
+
return name
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def remove_bookmark(doc, name: str) -> bool:
|
|
156
|
+
body = doc.element.body
|
|
157
|
+
removed = False
|
|
158
|
+
for start in list(body.iter(qn("w:bookmarkStart"))):
|
|
159
|
+
if start.get(qn("w:name")) != name:
|
|
160
|
+
continue
|
|
161
|
+
bookmark_id = start.get(qn("w:id"))
|
|
162
|
+
start.getparent().remove(start)
|
|
163
|
+
removed = True
|
|
164
|
+
if bookmark_id is not None:
|
|
165
|
+
for end in list(body.iter(qn("w:bookmarkEnd"))):
|
|
166
|
+
if end.get(qn("w:id")) == bookmark_id:
|
|
167
|
+
end.getparent().remove(end)
|
|
168
|
+
return removed
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def is_tool_bookmark(name: str) -> bool:
|
|
172
|
+
"""判断是否本工具写入的书签(Table_ / 旧 CPAH_ / 旧 TBL_ / 标注 MRK_ 前缀)。
|
|
173
|
+
|
|
174
|
+
Word 自带书签(如 ``_Toc…`` 目录书签)不在此列,清理时保留不动。
|
|
175
|
+
"""
|
|
176
|
+
return (
|
|
177
|
+
name.startswith(BOOKMARK_PREFIX)
|
|
178
|
+
or name.startswith("CPAH_")
|
|
179
|
+
or bool(LEGACY_BOOKMARK_RE.match(name))
|
|
180
|
+
or name.startswith("MRK_")
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def clear_tool_bookmarks(doc) -> dict:
|
|
185
|
+
"""删除文档里所有本工具书签(start 与配对的 end 一起移除)。
|
|
186
|
+
|
|
187
|
+
返回 ``{"removed": int, "kept": int, "names": [...]}``:removed 为本工具书签数,
|
|
188
|
+
kept 为保留的 Word 自带书签数。
|
|
189
|
+
"""
|
|
190
|
+
body = doc.element.body
|
|
191
|
+
names = bookmark_names(body)
|
|
192
|
+
tool_names = sorted(n for n in names if is_tool_bookmark(n))
|
|
193
|
+
kept = len(names) - len(tool_names)
|
|
194
|
+
removed = 0
|
|
195
|
+
for name in tool_names:
|
|
196
|
+
if remove_bookmark(doc, name):
|
|
197
|
+
removed += 1
|
|
198
|
+
return {"removed": removed, "kept": kept, "names": tool_names}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def bookmark_starts_in(p_el) -> list[str]:
|
|
202
|
+
return [b.get(qn("w:name")) for b in p_el.iter(qn("w:bookmarkStart")) if b.get(qn("w:name"))]
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _remove_link_fields(p_el) -> int:
|
|
206
|
+
"""删除段落里由本工具写入的 HYPERLINK 域(Word 会把超链接规范化为域)。"""
|
|
207
|
+
removed = 0
|
|
208
|
+
runs = list(p_el.findall(qn("w:r")))
|
|
209
|
+
index = 0
|
|
210
|
+
while index < len(runs):
|
|
211
|
+
fld = runs[index].find(qn("w:fldChar"))
|
|
212
|
+
if fld is None or fld.get(qn("w:fldCharType")) != "begin":
|
|
213
|
+
index += 1
|
|
214
|
+
continue
|
|
215
|
+
depth = 0
|
|
216
|
+
end = index
|
|
217
|
+
instr_parts: list[str] = []
|
|
218
|
+
display_parts: list[str] = []
|
|
219
|
+
separated = False
|
|
220
|
+
while end < len(runs):
|
|
221
|
+
inner = runs[end].find(qn("w:fldChar"))
|
|
222
|
+
if inner is not None:
|
|
223
|
+
fld_type = inner.get(qn("w:fldCharType"))
|
|
224
|
+
if fld_type == "begin":
|
|
225
|
+
depth += 1
|
|
226
|
+
elif fld_type == "end":
|
|
227
|
+
depth -= 1
|
|
228
|
+
elif fld_type == "separate":
|
|
229
|
+
separated = True
|
|
230
|
+
for node in runs[end].findall(qn("w:instrText")):
|
|
231
|
+
instr_parts.append(node.text or "")
|
|
232
|
+
if separated:
|
|
233
|
+
for node in runs[end].findall(qn("w:t")):
|
|
234
|
+
display_parts.append(node.text or "")
|
|
235
|
+
if depth == 0 and end > index:
|
|
236
|
+
break
|
|
237
|
+
end += 1
|
|
238
|
+
instr = "".join(instr_parts)
|
|
239
|
+
display = "".join(display_parts)
|
|
240
|
+
ours = (
|
|
241
|
+
EXCEL_LINK_TEXT in display
|
|
242
|
+
or "_表格数据.xlsx" in instr
|
|
243
|
+
or '\\l "EX_T' in instr
|
|
244
|
+
)
|
|
245
|
+
if ours and "HYPERLINK" in instr.upper():
|
|
246
|
+
for run in runs[index : end + 1]:
|
|
247
|
+
p_el.remove(run)
|
|
248
|
+
removed += 1
|
|
249
|
+
runs = list(p_el.findall(qn("w:r")))
|
|
250
|
+
continue
|
|
251
|
+
index = end + 1
|
|
252
|
+
return removed
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _strip_marker_from_paragraph(p_el) -> int:
|
|
256
|
+
"""删除段落文本中的〔→Excel〕(可能被 Word 拆成多个 run),其余文字与格式不动。"""
|
|
257
|
+
removed = 0
|
|
258
|
+
while True:
|
|
259
|
+
runs = list(p_el.findall(qn("w:r")))
|
|
260
|
+
full = "".join("".join(t.text or "" for t in run.findall(qn("w:t"))) for run in runs)
|
|
261
|
+
start = full.find(EXCEL_LINK_TEXT)
|
|
262
|
+
if start < 0:
|
|
263
|
+
return removed
|
|
264
|
+
end = start + len(EXCEL_LINK_TEXT)
|
|
265
|
+
removed += 1
|
|
266
|
+
offset = 0
|
|
267
|
+
for run in runs:
|
|
268
|
+
texts = run.findall(qn("w:t"))
|
|
269
|
+
run_text = "".join(t.text or "" for t in texts)
|
|
270
|
+
run_start, run_end = offset, offset + len(run_text)
|
|
271
|
+
offset = run_end
|
|
272
|
+
if run_end <= start or run_start >= end:
|
|
273
|
+
continue
|
|
274
|
+
new_text = run_text[: max(0, start - run_start)] + run_text[max(0, end - run_start) :]
|
|
275
|
+
if new_text == "" and run.find(qn("w:drawing")) is None:
|
|
276
|
+
p_el.remove(run)
|
|
277
|
+
continue
|
|
278
|
+
for idx, node in enumerate(texts):
|
|
279
|
+
node.text = new_text if idx == 0 else ""
|
|
280
|
+
node.set(qn("xml:space"), "preserve")
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def cleanup_excel_markers(doc) -> int:
|
|
284
|
+
"""删除 Word 里遗留的〔→Excel〕文字与超链接域,段落其余内容与格式不动。"""
|
|
285
|
+
removed = 0
|
|
286
|
+
for p_el in doc.element.body.iter(qn("w:p")):
|
|
287
|
+
for hyperlink in list(p_el.findall(qn("w:hyperlink"))):
|
|
288
|
+
text = "".join(t.text or "" for t in hyperlink.iter(qn("w:t")))
|
|
289
|
+
if EXCEL_LINK_TEXT in text:
|
|
290
|
+
p_el.remove(hyperlink)
|
|
291
|
+
removed += 1
|
|
292
|
+
removed += _remove_link_fields(p_el)
|
|
293
|
+
removed += _strip_marker_from_paragraph(p_el)
|
|
294
|
+
return removed
|