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,456 @@
|
|
|
1
|
+
"""批量同步页:多报告批量同步 / 模板生成(单模板或多模板分组)两个 tab。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from PySide6.QtCore import Qt, QTimer
|
|
8
|
+
from PySide6.QtWidgets import (
|
|
9
|
+
QComboBox,
|
|
10
|
+
QFileDialog,
|
|
11
|
+
QHBoxLayout,
|
|
12
|
+
QHeaderView,
|
|
13
|
+
QLabel,
|
|
14
|
+
QLineEdit,
|
|
15
|
+
QListWidget,
|
|
16
|
+
QListWidgetItem,
|
|
17
|
+
QPushButton,
|
|
18
|
+
QRadioButton,
|
|
19
|
+
QTabWidget,
|
|
20
|
+
QTableWidget,
|
|
21
|
+
QTableWidgetItem,
|
|
22
|
+
QVBoxLayout,
|
|
23
|
+
QWidget,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
from ... import api
|
|
27
|
+
from ..widgets.cards import make_action_bar, make_checkbutton
|
|
28
|
+
from ._page import BasePage
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class BatchPage(BasePage):
|
|
32
|
+
title = "批量同步"
|
|
33
|
+
subtitle = "多份报告一次同步,或按模板批量生成新 Word;原件不改。"
|
|
34
|
+
|
|
35
|
+
def build(self, body: QWidget) -> None:
|
|
36
|
+
card = self.card(body)
|
|
37
|
+
tabs = QTabWidget()
|
|
38
|
+
card.layout().addWidget(tabs)
|
|
39
|
+
tabs.addTab(self._build_sync_tab(), "多报告批量同步")
|
|
40
|
+
tabs.addTab(self._build_template_tab(), "模板生成")
|
|
41
|
+
self._waiting: str | None = None
|
|
42
|
+
self._results: list[dict] = []
|
|
43
|
+
self._timer = QTimer(self)
|
|
44
|
+
self._timer.timeout.connect(self._check_done)
|
|
45
|
+
self._timer.start(300)
|
|
46
|
+
|
|
47
|
+
def _check_done(self) -> None:
|
|
48
|
+
if not self._waiting or self.ctx.busy:
|
|
49
|
+
return
|
|
50
|
+
mode, self._waiting = self._waiting, None
|
|
51
|
+
if mode == "sync":
|
|
52
|
+
by_path = {r["docx"]: r for r in self._results}
|
|
53
|
+
for row in range(self.sync_table.rowCount()):
|
|
54
|
+
path = self.sync_table.item(row, 0).data(Qt.UserRole)
|
|
55
|
+
r = by_path.get(path)
|
|
56
|
+
if r:
|
|
57
|
+
item = QTableWidgetItem("✔ 成功" if r["status"] == "ok" else "✘ 已跳过")
|
|
58
|
+
if r["status"] != "ok":
|
|
59
|
+
item.setToolTip(r["reason"])
|
|
60
|
+
self.sync_table.setItem(row, 2, item)
|
|
61
|
+
else:
|
|
62
|
+
for row, r in enumerate(self._results):
|
|
63
|
+
if row >= self.tpl_table.rowCount():
|
|
64
|
+
break
|
|
65
|
+
text = "✔ 已生成" if r["status"] == "ok" and r["output"] else (
|
|
66
|
+
"✔ 预演完成" if r["status"] == "ok" else "✘ 失败")
|
|
67
|
+
item = QTableWidgetItem(text)
|
|
68
|
+
if r["status"] != "ok":
|
|
69
|
+
item.setToolTip(r["reason"])
|
|
70
|
+
self.tpl_table.setItem(row, 2, item)
|
|
71
|
+
|
|
72
|
+
@staticmethod
|
|
73
|
+
def _make_table(headers: list[str]) -> QTableWidget:
|
|
74
|
+
table = QTableWidget(0, len(headers))
|
|
75
|
+
table.setHorizontalHeaderLabels(headers)
|
|
76
|
+
table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
|
|
77
|
+
table.horizontalHeader().setSectionResizeMode(len(headers) - 1, QHeaderView.Stretch)
|
|
78
|
+
table.setSelectionBehavior(QTableWidget.SelectRows)
|
|
79
|
+
table.setEditTriggers(QTableWidget.NoEditTriggers)
|
|
80
|
+
table.setMinimumHeight(160)
|
|
81
|
+
return table
|
|
82
|
+
|
|
83
|
+
# ---------- tab 1:多报告批量同步 ----------
|
|
84
|
+
|
|
85
|
+
def _build_sync_tab(self) -> QWidget:
|
|
86
|
+
tab = QWidget()
|
|
87
|
+
layout = QVBoxLayout(tab)
|
|
88
|
+
layout.setContentsMargins(12, 12, 12, 12)
|
|
89
|
+
layout.setSpacing(10)
|
|
90
|
+
self.tip_label(tab, "自动配对同名工作簿;单份失败只跳过该份。")
|
|
91
|
+
content_row = QHBoxLayout()
|
|
92
|
+
content_row.setSpacing(8)
|
|
93
|
+
content_row.addWidget(QLabel("同步内容:"))
|
|
94
|
+
self.batch_tables = make_checkbutton(tab, "表格", True)
|
|
95
|
+
self.batch_marks = make_checkbutton(tab, "标注", False)
|
|
96
|
+
self.batch_placeholders = make_checkbutton(tab, "占位符", False)
|
|
97
|
+
for box in (self.batch_tables, self.batch_marks, self.batch_placeholders):
|
|
98
|
+
content_row.addWidget(box)
|
|
99
|
+
content_row.addStretch(1)
|
|
100
|
+
layout.addLayout(content_row)
|
|
101
|
+
self.batch_keep_highlight = make_checkbutton(tab, "保留标注高亮", True)
|
|
102
|
+
self.batch_keep_highlight.setVisible(False)
|
|
103
|
+
self.batch_marks.toggled.connect(self.batch_keep_highlight.setVisible)
|
|
104
|
+
layout.addWidget(self.batch_keep_highlight)
|
|
105
|
+
row = QHBoxLayout()
|
|
106
|
+
add = QPushButton("添加报告")
|
|
107
|
+
add.clicked.connect(lambda _checked=False: self._add_reports())
|
|
108
|
+
row.addWidget(add)
|
|
109
|
+
remove = QPushButton("移除选中行")
|
|
110
|
+
remove.clicked.connect(lambda _checked=False: self._remove_rows(self.sync_table))
|
|
111
|
+
row.addWidget(remove)
|
|
112
|
+
row.addStretch(1)
|
|
113
|
+
layout.addLayout(row)
|
|
114
|
+
self.sync_table = self._make_table(["报告(勾选参与)", "工作簿", "状态"])
|
|
115
|
+
layout.addWidget(self.sync_table)
|
|
116
|
+
self.sync_backup = make_checkbutton(tab, "同步前备份;关闭则直接覆盖", False)
|
|
117
|
+
layout.addWidget(self.sync_backup)
|
|
118
|
+
sync_controls = {}
|
|
119
|
+
self.bind_defaults(layout, {
|
|
120
|
+
"sync_tables": self.batch_tables, "sync_marks": self.batch_marks,
|
|
121
|
+
"sync_placeholders": self.batch_placeholders,
|
|
122
|
+
"keep_highlight": self.batch_keep_highlight, "auto_backup": self.sync_backup,
|
|
123
|
+
}, [("表格", "table_zero"), ("标注", "mark_zero"), ("占位符", "placeholder_zero")], sync_controls)
|
|
124
|
+
self.table_zero = sync_controls["table_zero"]
|
|
125
|
+
self.mark_zero = sync_controls["mark_zero"]
|
|
126
|
+
self.placeholder_zero = sync_controls["placeholder_zero"]
|
|
127
|
+
self.button_row(tab, lambda: self._start_sync(True), lambda: self._start_sync(False), "全部仅预演", "开始批量同步")
|
|
128
|
+
layout.addStretch(1)
|
|
129
|
+
return tab
|
|
130
|
+
|
|
131
|
+
def _add_reports(self) -> None:
|
|
132
|
+
paths, _ = QFileDialog.getOpenFileNames(self, "选择要批量同步的 Word 报告", "", "Word 文档 (*.docx)")
|
|
133
|
+
existing = {self.sync_table.item(r, 0).data(Qt.UserRole) for r in range(self.sync_table.rowCount())}
|
|
134
|
+
for p in paths:
|
|
135
|
+
if p in existing:
|
|
136
|
+
continue
|
|
137
|
+
row = self.sync_table.rowCount()
|
|
138
|
+
self.sync_table.insertRow(row)
|
|
139
|
+
item = QTableWidgetItem(Path(p).name)
|
|
140
|
+
item.setData(Qt.UserRole, p)
|
|
141
|
+
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
|
|
142
|
+
item.setCheckState(Qt.Checked)
|
|
143
|
+
self.sync_table.setItem(row, 0, item)
|
|
144
|
+
xlsx = api.workbook_path(p)
|
|
145
|
+
note = xlsx.name if xlsx.exists() else f"{xlsx.name}(未找到,将跳过)"
|
|
146
|
+
self.sync_table.setItem(row, 1, QTableWidgetItem(note))
|
|
147
|
+
self.sync_table.setItem(row, 2, QTableWidgetItem("待执行"))
|
|
148
|
+
|
|
149
|
+
@staticmethod
|
|
150
|
+
def _remove_rows(table: QTableWidget) -> None:
|
|
151
|
+
for row in sorted({i.row() for i in table.selectedIndexes()}, reverse=True):
|
|
152
|
+
table.removeRow(row)
|
|
153
|
+
|
|
154
|
+
def _start_sync(self, dry_run: bool) -> None:
|
|
155
|
+
contents = tuple(
|
|
156
|
+
key for key, box in (
|
|
157
|
+
("tables", self.batch_tables), ("marks", self.batch_marks), ("placeholders", self.batch_placeholders)
|
|
158
|
+
) if box.isChecked()
|
|
159
|
+
)
|
|
160
|
+
if not contents:
|
|
161
|
+
self.ctx.error("未选择同步内容", "请至少勾选一项同步内容(表格/标注/占位符)。")
|
|
162
|
+
return
|
|
163
|
+
content_text = "+".join(
|
|
164
|
+
{"tables": "表格", "marks": "标注", "placeholders": "占位符"}[key] for key in contents
|
|
165
|
+
)
|
|
166
|
+
paths = [
|
|
167
|
+
self.sync_table.item(r, 0).data(Qt.UserRole)
|
|
168
|
+
for r in range(self.sync_table.rowCount())
|
|
169
|
+
if self.sync_table.item(r, 0).checkState() == Qt.Checked
|
|
170
|
+
]
|
|
171
|
+
if not paths:
|
|
172
|
+
self.ctx.info("未选择报告", "请先点「添加报告」并勾选要同步的报告。")
|
|
173
|
+
return
|
|
174
|
+
if not dry_run and not self.ctx.confirm(
|
|
175
|
+
"确认批量同步",
|
|
176
|
+
f"将把 {len(paths)} 份报告各自的工作簿改动回写到 Word。\n同步内容:{content_text}\n"
|
|
177
|
+
+ ("同步前会备份有改动的文件。" if self.sync_backup.isChecked() else "未勾选备份:直接覆盖原文件。")
|
|
178
|
+
+ "\n\n请先关闭这些 Word 与 Excel。确认开始?",
|
|
179
|
+
):
|
|
180
|
+
return
|
|
181
|
+
backup = self.sync_backup.isChecked()
|
|
182
|
+
keep_highlight = self.batch_keep_highlight.isChecked()
|
|
183
|
+
table_zero = self.table_zero.currentData()
|
|
184
|
+
mark_zero = self.mark_zero.currentData()
|
|
185
|
+
placeholder_zero = self.placeholder_zero.currentData()
|
|
186
|
+
|
|
187
|
+
def run() -> int:
|
|
188
|
+
self._results = api.run_batch_writeback(
|
|
189
|
+
paths, dry_run=dry_run, backup=backup, contents=contents, keep_highlight=keep_highlight,
|
|
190
|
+
table_zero=table_zero, mark_zero=mark_zero,
|
|
191
|
+
placeholder_zero=placeholder_zero,
|
|
192
|
+
)
|
|
193
|
+
return 0
|
|
194
|
+
|
|
195
|
+
if self.ctx.run_steps([(("批量同步(预演)" if dry_run else "批量同步") + f"·{content_text}", run)]):
|
|
196
|
+
self._waiting = "sync"
|
|
197
|
+
for r in range(self.sync_table.rowCount()):
|
|
198
|
+
self.sync_table.setItem(r, 2, QTableWidgetItem("执行中…"))
|
|
199
|
+
|
|
200
|
+
# ---------- tab 2:模板生成 ----------
|
|
201
|
+
|
|
202
|
+
def _build_template_tab(self) -> QWidget:
|
|
203
|
+
tab = QWidget()
|
|
204
|
+
layout = QVBoxLayout(tab)
|
|
205
|
+
layout.setContentsMargins(12, 12, 12, 12)
|
|
206
|
+
layout.setSpacing(10)
|
|
207
|
+
self.tip_label(
|
|
208
|
+
tab,
|
|
209
|
+
"按模板生成新 Word,不改原件;工作簿须与模板对应。占位符需先在「正文信息」填齐。",
|
|
210
|
+
)
|
|
211
|
+
content_row = QHBoxLayout()
|
|
212
|
+
content_row.setSpacing(8)
|
|
213
|
+
content_row.addWidget(QLabel("同步内容:"))
|
|
214
|
+
self.tpl_tables = make_checkbutton(tab, "表格", True)
|
|
215
|
+
self.tpl_marks = make_checkbutton(tab, "标注", False)
|
|
216
|
+
self.tpl_placeholders = make_checkbutton(tab, "占位符", True)
|
|
217
|
+
for box in (self.tpl_tables, self.tpl_marks, self.tpl_placeholders):
|
|
218
|
+
content_row.addWidget(box)
|
|
219
|
+
content_row.addStretch(1)
|
|
220
|
+
layout.addLayout(content_row)
|
|
221
|
+
self.tpl_keep_highlight = make_checkbutton(tab, "保留标注高亮", True)
|
|
222
|
+
self.tpl_keep_highlight.setVisible(False)
|
|
223
|
+
self.tpl_marks.toggled.connect(self.tpl_keep_highlight.setVisible)
|
|
224
|
+
layout.addWidget(self.tpl_keep_highlight)
|
|
225
|
+
tpl_controls = {}
|
|
226
|
+
self.bind_defaults(layout, {
|
|
227
|
+
"keep_highlight": self.tpl_keep_highlight,
|
|
228
|
+
}, [
|
|
229
|
+
("表格", "table_zero"), ("标注", "mark_zero"), ("占位符", "placeholder_zero")
|
|
230
|
+
], tpl_controls)
|
|
231
|
+
self.tpl_table_zero = tpl_controls["table_zero"]
|
|
232
|
+
self.tpl_mark_zero = tpl_controls["mark_zero"]
|
|
233
|
+
self.tpl_placeholder_zero = tpl_controls["placeholder_zero"]
|
|
234
|
+
mode_row = QHBoxLayout()
|
|
235
|
+
self.mode_single = QRadioButton("单模板")
|
|
236
|
+
self.mode_single.setChecked(True)
|
|
237
|
+
self.mode_multi = QRadioButton("多模板")
|
|
238
|
+
self.mode_single.toggled.connect(lambda _c: self._mode_changed())
|
|
239
|
+
mode_row.addWidget(self.mode_single)
|
|
240
|
+
mode_row.addWidget(self.mode_multi)
|
|
241
|
+
mode_row.addStretch(1)
|
|
242
|
+
layout.addLayout(mode_row)
|
|
243
|
+
|
|
244
|
+
tpl_row = QHBoxLayout()
|
|
245
|
+
self.btn_pick_tpl = QPushButton("选择模板")
|
|
246
|
+
self.btn_pick_tpl.clicked.connect(lambda _checked=False: self._pick_templates())
|
|
247
|
+
tpl_row.addWidget(self.btn_pick_tpl)
|
|
248
|
+
self.btn_del_tpl = QPushButton("移除选中模板")
|
|
249
|
+
self.btn_del_tpl.clicked.connect(lambda _checked=False: self._remove_template())
|
|
250
|
+
tpl_row.addWidget(self.btn_del_tpl)
|
|
251
|
+
tpl_row.addStretch(1)
|
|
252
|
+
layout.addLayout(tpl_row)
|
|
253
|
+
self.tpl_label = QLabel("尚未选择模板")
|
|
254
|
+
self.tpl_label.setObjectName("CardDesc")
|
|
255
|
+
self.tpl_label.setWordWrap(True)
|
|
256
|
+
self.tpl_label.setMinimumWidth(0)
|
|
257
|
+
layout.addWidget(self.tpl_label)
|
|
258
|
+
self.tpl_list = QListWidget()
|
|
259
|
+
self.tpl_list.setMaximumHeight(90)
|
|
260
|
+
layout.addWidget(self.tpl_list)
|
|
261
|
+
|
|
262
|
+
actions = make_action_bar(
|
|
263
|
+
tab,
|
|
264
|
+
"生成正文信息填写表",
|
|
265
|
+
self._fill_placeholder_sheets,
|
|
266
|
+
secondary=[
|
|
267
|
+
("添加 Excel 工作簿", self._add_workbooks),
|
|
268
|
+
("移除选中行", lambda: self._remove_rows(self.tpl_table)),
|
|
269
|
+
],
|
|
270
|
+
primary_index=2,
|
|
271
|
+
)
|
|
272
|
+
actions.buttons[2].setToolTip("按模板里的 {字段名} 占位符,给各工作簿加入/补齐「正文信息」表(保留已填值),供填写正文内容。仅在实际新增字段时写盘,备份按全局设置执行。")
|
|
273
|
+
layout.addWidget(actions)
|
|
274
|
+
self.tpl_table = self._make_table(["Excel 工作簿", "所属模板", "状态"])
|
|
275
|
+
self.tpl_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeToContents)
|
|
276
|
+
layout.addWidget(self.tpl_table)
|
|
277
|
+
|
|
278
|
+
out_row = QHBoxLayout()
|
|
279
|
+
out_row.addWidget(QLabel("输出目录"))
|
|
280
|
+
self.out_edit = QLineEdit()
|
|
281
|
+
out_row.addWidget(self.out_edit, 1)
|
|
282
|
+
browse = QPushButton("浏览…")
|
|
283
|
+
browse.clicked.connect(lambda _checked=False: self._pick_out_dir())
|
|
284
|
+
out_row.addWidget(browse)
|
|
285
|
+
layout.addLayout(out_row)
|
|
286
|
+
self.button_row(tab, lambda: self._start_tpl(True), lambda: self._start_tpl(False), "全部仅预演", "开始生成")
|
|
287
|
+
layout.addStretch(1)
|
|
288
|
+
self._mode_changed()
|
|
289
|
+
return tab
|
|
290
|
+
|
|
291
|
+
def _templates(self) -> list[str]:
|
|
292
|
+
if self.mode_single.isChecked():
|
|
293
|
+
text = self.tpl_label.text()
|
|
294
|
+
return [text] if text != "尚未选择模板" and Path(text).exists() else []
|
|
295
|
+
return [self.tpl_list.item(i).data(Qt.UserRole) for i in range(self.tpl_list.count())]
|
|
296
|
+
|
|
297
|
+
def _mode_changed(self) -> None:
|
|
298
|
+
multi = self.mode_multi.isChecked()
|
|
299
|
+
self.tpl_list.setVisible(multi)
|
|
300
|
+
self.tpl_label.setVisible(not multi)
|
|
301
|
+
self.btn_del_tpl.setVisible(multi)
|
|
302
|
+
self.btn_pick_tpl.setText("添加模板" if multi else "选择模板")
|
|
303
|
+
self.tpl_table.setColumnHidden(1, not multi)
|
|
304
|
+
self._refresh_tpl_combos()
|
|
305
|
+
|
|
306
|
+
def _pick_templates(self) -> None:
|
|
307
|
+
if self.mode_single.isChecked():
|
|
308
|
+
path, _ = QFileDialog.getOpenFileName(self, "选择 Word 模板", "", "Word 文档 (*.docx)")
|
|
309
|
+
if path:
|
|
310
|
+
self.tpl_label.setText(path)
|
|
311
|
+
if not self.out_edit.text():
|
|
312
|
+
self.out_edit.setText(str(Path(path).parent))
|
|
313
|
+
else:
|
|
314
|
+
paths, _ = QFileDialog.getOpenFileNames(self, "添加 Word 模板(按添加顺序编号)", "", "Word 文档 (*.docx)")
|
|
315
|
+
existing = self._templates()
|
|
316
|
+
for p in paths:
|
|
317
|
+
if p not in existing:
|
|
318
|
+
item = QListWidgetItem(f"模板{self.tpl_list.count() + 1} · {Path(p).name}")
|
|
319
|
+
item.setData(Qt.UserRole, p)
|
|
320
|
+
self.tpl_list.addItem(item)
|
|
321
|
+
if self._templates() and not self.out_edit.text():
|
|
322
|
+
self.out_edit.setText(str(Path(self._templates()[0]).parent))
|
|
323
|
+
self._refresh_tpl_combos()
|
|
324
|
+
|
|
325
|
+
def _remove_template(self) -> None:
|
|
326
|
+
for item in self.tpl_list.selectedItems():
|
|
327
|
+
self.tpl_list.takeItem(self.tpl_list.row(item))
|
|
328
|
+
for i in range(self.tpl_list.count()):
|
|
329
|
+
path = self.tpl_list.item(i).data(Qt.UserRole)
|
|
330
|
+
self.tpl_list.item(i).setText(f"模板{i + 1} · {Path(path).name}")
|
|
331
|
+
self._refresh_tpl_combos()
|
|
332
|
+
|
|
333
|
+
def _refresh_tpl_combos(self) -> None:
|
|
334
|
+
n = len(self._templates())
|
|
335
|
+
for row in range(self.tpl_table.rowCount()):
|
|
336
|
+
combo = self.tpl_table.cellWidget(row, 1)
|
|
337
|
+
if combo is None:
|
|
338
|
+
continue
|
|
339
|
+
current = combo.currentIndex()
|
|
340
|
+
combo.clear()
|
|
341
|
+
combo.addItems([f"模板{i + 1}" for i in range(n)])
|
|
342
|
+
if 0 <= current < n:
|
|
343
|
+
combo.setCurrentIndex(current)
|
|
344
|
+
|
|
345
|
+
def _add_workbooks(self) -> None:
|
|
346
|
+
paths, _ = QFileDialog.getOpenFileNames(self, "选择 Excel 工作簿", "", "Excel 工作簿 (*.xlsx)")
|
|
347
|
+
existing = {self.tpl_table.item(r, 0).data(Qt.UserRole) for r in range(self.tpl_table.rowCount())}
|
|
348
|
+
for p in paths:
|
|
349
|
+
if p in existing:
|
|
350
|
+
continue
|
|
351
|
+
row = self.tpl_table.rowCount()
|
|
352
|
+
self.tpl_table.insertRow(row)
|
|
353
|
+
item = QTableWidgetItem(Path(p).name)
|
|
354
|
+
item.setData(Qt.UserRole, p)
|
|
355
|
+
self.tpl_table.setItem(row, 0, item)
|
|
356
|
+
combo = QComboBox()
|
|
357
|
+
combo.addItems([f"模板{i + 1}" for i in range(len(self._templates()))])
|
|
358
|
+
self.tpl_table.setCellWidget(row, 1, combo)
|
|
359
|
+
self.tpl_table.setItem(row, 2, QTableWidgetItem("待执行"))
|
|
360
|
+
|
|
361
|
+
def _pick_out_dir(self) -> None:
|
|
362
|
+
path = QFileDialog.getExistingDirectory(self, "选择输出目录", self.out_edit.text() or "")
|
|
363
|
+
if path:
|
|
364
|
+
self.out_edit.setText(path)
|
|
365
|
+
|
|
366
|
+
def _fill_placeholder_sheets(self) -> None:
|
|
367
|
+
templates = self._templates()
|
|
368
|
+
if not templates:
|
|
369
|
+
self.ctx.info("未选择模板", "请先选择 Word 模板,再据其占位符生成填写表。")
|
|
370
|
+
return
|
|
371
|
+
if self.tpl_table.rowCount() == 0:
|
|
372
|
+
self.ctx.info("未添加工作簿", "请先点「添加 Excel 工作簿」。")
|
|
373
|
+
return
|
|
374
|
+
groups: dict[int, list[str]] = {}
|
|
375
|
+
unassigned = 0
|
|
376
|
+
for row in range(self.tpl_table.rowCount()):
|
|
377
|
+
combo = self.tpl_table.cellWidget(row, 1)
|
|
378
|
+
tpl_no = 1 if self.mode_single.isChecked() else (combo.currentIndex() + 1 if combo else 0)
|
|
379
|
+
path = self.tpl_table.item(row, 0).data(Qt.UserRole)
|
|
380
|
+
if not 1 <= tpl_no <= len(templates):
|
|
381
|
+
unassigned += 1
|
|
382
|
+
continue
|
|
383
|
+
groups.setdefault(tpl_no, []).append(path)
|
|
384
|
+
if not groups:
|
|
385
|
+
self.ctx.info("未指定模板", "请先在「所属模板」列给工作簿选择模板。")
|
|
386
|
+
return
|
|
387
|
+
total = sum(len(v) for v in groups.values())
|
|
388
|
+
backup = self.ctx.global_settings()["auto_backup"]
|
|
389
|
+
msg = (f"将按模板里的 {{字段名}} 占位符,给 {total} 个工作簿加入/补齐「正文信息」表(保留已填值)。\n\n"
|
|
390
|
+
"字段已齐全时不改文件;实际补入字段时会修改源工作簿,请先关闭这些 Excel。\n"
|
|
391
|
+
+ ("写盘前按全局设置自动备份。" if backup else "全局设置已关闭备份,本次将直接保存。"))
|
|
392
|
+
if unassigned:
|
|
393
|
+
msg += f"\n另有 {unassigned} 个未指定模板的工作簿将跳过。"
|
|
394
|
+
if not self.ctx.confirm("生成正文信息填写表", msg + "\n\n确认开始?"):
|
|
395
|
+
return
|
|
396
|
+
|
|
397
|
+
def run() -> int:
|
|
398
|
+
for tpl_no, paths in groups.items():
|
|
399
|
+
api.fill_placeholder_sheets(
|
|
400
|
+
templates[tpl_no - 1], paths, backup=backup
|
|
401
|
+
)
|
|
402
|
+
return 0
|
|
403
|
+
|
|
404
|
+
self.ctx.run_steps([("生成正文信息填写表", run)])
|
|
405
|
+
|
|
406
|
+
def _start_tpl(self, dry_run: bool) -> None:
|
|
407
|
+
contents = tuple(
|
|
408
|
+
key for key, box in (
|
|
409
|
+
("tables", self.tpl_tables), ("marks", self.tpl_marks), ("placeholders", self.tpl_placeholders)
|
|
410
|
+
) if box.isChecked()
|
|
411
|
+
)
|
|
412
|
+
if not contents:
|
|
413
|
+
self.ctx.error("未选择同步内容", "请至少勾选一项同步内容(表格/标注/占位符)。")
|
|
414
|
+
return
|
|
415
|
+
content_text = "+".join(
|
|
416
|
+
{"tables": "表格", "marks": "标注", "placeholders": "占位符"}[key] for key in contents
|
|
417
|
+
)
|
|
418
|
+
templates = self._templates()
|
|
419
|
+
if not templates:
|
|
420
|
+
self.ctx.info("未选择模板", "请先选择 Word 模板。")
|
|
421
|
+
return
|
|
422
|
+
if self.tpl_table.rowCount() == 0:
|
|
423
|
+
self.ctx.info("未添加工作簿", "请先点「添加 Excel 工作簿」。")
|
|
424
|
+
return
|
|
425
|
+
out_dir = self.out_edit.text().strip()
|
|
426
|
+
if not out_dir:
|
|
427
|
+
self.ctx.info("未指定输出目录", "请先指定生成 Word 的输出目录。")
|
|
428
|
+
return
|
|
429
|
+
items = []
|
|
430
|
+
for row in range(self.tpl_table.rowCount()):
|
|
431
|
+
combo = self.tpl_table.cellWidget(row, 1)
|
|
432
|
+
tpl_no = 1 if self.mode_single.isChecked() else (combo.currentIndex() + 1 if combo else 0)
|
|
433
|
+
items.append({"excel": self.tpl_table.item(row, 0).data(Qt.UserRole), "template": tpl_no})
|
|
434
|
+
if not dry_run and not self.ctx.confirm(
|
|
435
|
+
"确认模板生成",
|
|
436
|
+
f"将用 {len(templates)} 个模板为 {len(items)} 个工作簿各生成一份新 Word:\n\n同步内容:{content_text}\n输出目录:{out_dir}\n\n"
|
|
437
|
+
"模板原件与源 Excel 不会改动;重名输出自动加序号。确认开始?",
|
|
438
|
+
):
|
|
439
|
+
return
|
|
440
|
+
keep_highlight = self.tpl_keep_highlight.isChecked()
|
|
441
|
+
table_zero = self.tpl_table_zero.currentData()
|
|
442
|
+
mark_zero = self.tpl_mark_zero.currentData()
|
|
443
|
+
placeholder_zero = self.tpl_placeholder_zero.currentData()
|
|
444
|
+
|
|
445
|
+
def run() -> int:
|
|
446
|
+
self._results = api.run_template_batch(
|
|
447
|
+
templates, items, out_dir, dry_run=dry_run, contents=contents, keep_highlight=keep_highlight,
|
|
448
|
+
table_zero=table_zero, mark_zero=mark_zero,
|
|
449
|
+
placeholder_zero=placeholder_zero,
|
|
450
|
+
)
|
|
451
|
+
return 0
|
|
452
|
+
|
|
453
|
+
if self.ctx.run_steps([(("模板生成(预演)" if dry_run else "模板生成") + f"·{content_text}", run)]):
|
|
454
|
+
self._waiting = "tpl"
|
|
455
|
+
for r in range(self.tpl_table.rowCount()):
|
|
456
|
+
self.tpl_table.setItem(r, 2, QTableWidgetItem("执行中…"))
|