-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
471 lines (441 loc) · 13.6 KB
/
index.html
File metadata and controls
471 lines (441 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TXT 到 DOCX 域占位符写入</title>
<style>
:root {
--bg: #f5f2ea;
--ink: #1f1b16;
--muted: #6f6358;
--card: #ffffff;
--accent: #2e6f72;
--accent-2: #b77b45;
--border: #e1d8cc;
--shadow: 0 16px 40px rgba(31, 27, 22, 0.12);
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: "Times New Roman", "Noto Serif SC", "Songti SC", serif;
color: var(--ink);
background: radial-gradient(circle at 10% 10%, #fef9f2 0%, #f5f2ea 35%, #efe6d8 100%);
}
header {
padding: 28px 24px 12px;
text-align: center;
}
header h1 {
margin: 0 0 8px;
font-size: 28px;
letter-spacing: 1px;
}
header p {
margin: 0;
color: var(--muted);
font-size: 14px;
}
main {
max-width: 980px;
margin: 0 auto;
padding: 16px 24px 48px;
}
.panel {
background: var(--card);
border: 1px solid var(--border);
border-radius: 16px;
padding: 18px 20px;
box-shadow: var(--shadow);
margin-bottom: 18px;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 16px;
}
label {
font-size: 13px;
color: var(--muted);
display: block;
margin-bottom: 6px;
}
input[type="file"] {
width: 100%;
padding: 10px;
border: 1px dashed var(--border);
border-radius: 10px;
background: #fbfaf7;
}
button {
appearance: none;
border: none;
padding: 12px 18px;
border-radius: 999px;
font-size: 14px;
font-weight: 600;
letter-spacing: 0.5px;
background: linear-gradient(120deg, var(--accent), #255b5e);
color: #fff;
cursor: pointer;
box-shadow: 0 8px 20px rgba(46, 111, 114, 0.22);
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.status {
font-size: 13px;
color: var(--muted);
margin-top: 8px;
}
.list {
border: 1px solid var(--border);
border-radius: 12px;
padding: 12px 14px;
min-height: 120px;
background: #fcfbf8;
}
.list ul {
margin: 0;
padding-left: 18px;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
}
th, td {
text-align: left;
padding: 8px 10px;
border-bottom: 1px solid var(--border);
}
th {
color: var(--muted);
font-weight: 600;
}
.badge {
display: inline-block;
padding: 2px 8px;
border-radius: 999px;
background: #f2e6d6;
color: var(--accent-2);
font-size: 12px;
}
.download {
margin-top: 10px;
display: none;
}
.download a {
color: var(--accent);
text-decoration: none;
font-weight: 600;
}
</style>
</head>
<body>
<header>
<h1>TXT → DOCX 域占位符写入</h1>
<p>每行一条数据,填充文档中 5 个域占位符,并导出为 test_update.docx</p>
</header>
<main>
<section class="panel grid">
<div>
<label for="txtFile">TXT 数据文件(每行一条)</label>
<input id="txtFile" type="file" accept=".txt" />
</div>
<div>
<label for="docxFile">DOCX 模板(包含 5 个占位符)</label>
<input id="docxFile" type="file" accept=".docx" />
</div>
<div style="display:flex;align-items:flex-end;gap:12px;">
<button id="processBtn" type="button" disabled>读取并生成</button>
</div>
</section>
<section class="panel">
<div class="grid">
<div>
<div style="display:flex;align-items:center;gap:8px;margin-bottom:8px;">
<strong>读取到的数据</strong>
<span class="badge" id="dataCount">0 条</span>
</div>
<div class="list" id="dataList">
<ul></ul>
</div>
</div>
<div>
<div style="display:flex;align-items:center;gap:8px;margin-bottom:8px;">
<strong>识别到的域占位符</strong>
<span class="badge" id="placeholderCount">0 个</span>
</div>
<div class="list" id="placeholderList">
<ul></ul>
</div>
</div>
</div>
</section>
<section class="panel">
<div style="display:flex;align-items:center;gap:8px;margin-bottom:10px;">
<strong>写入映射(前 5 条数据)</strong>
<span class="badge" id="mappingCount">0 项</span>
</div>
<table id="mappingTable">
<thead>
<tr>
<th>占位符</th>
<th>写入数据</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div class="status" id="statusText">请先选择 TXT 和 DOCX 文件。</div>
<div class="download" id="downloadBox">
<a id="downloadLink" href="#" download="test_update.docx">下载 test_update.docx</a>
</div>
</section>
</main>
<script src="https://unpkg.com/pizzip@3.1.7/dist/pizzip.min.js"></script>
<script>
const txtInput = document.getElementById("txtFile");
const docxInput = document.getElementById("docxFile");
const processBtn = document.getElementById("processBtn");
const dataList = document.querySelector("#dataList ul");
const placeholderList = document.querySelector("#placeholderList ul");
const mappingTable = document.querySelector("#mappingTable tbody");
const statusText = document.getElementById("statusText");
const dataCount = document.getElementById("dataCount");
const placeholderCount = document.getElementById("placeholderCount");
const mappingCount = document.getElementById("mappingCount");
const downloadBox = document.getElementById("downloadBox");
const downloadLink = document.getElementById("downloadLink");
let txtLines = [];
let placeholders = [];
let docxArrayBuffer = null;
function updateButtonState() {
processBtn.disabled = !(txtInput.files.length && docxInput.files.length);
}
function renderList(listEl, items) {
listEl.innerHTML = "";
if (!items.length) {
listEl.innerHTML = "<li>暂无</li>";
return;
}
items.forEach((item, idx) => {
const li = document.createElement("li");
li.textContent = `${idx + 1}. ${item}`;
listEl.appendChild(li);
});
}
function renderMapping(mapping) {
mappingTable.innerHTML = "";
const entries = Object.entries(mapping);
mappingCount.textContent = `${entries.length} 项`;
if (!entries.length) {
const row = document.createElement("tr");
row.innerHTML = "<td colspan='2'>暂无映射</td>";
mappingTable.appendChild(row);
return;
}
entries.forEach(([key, value]) => {
const row = document.createElement("tr");
row.innerHTML = `<td>${key}</td><td>${value}</td>`;
mappingTable.appendChild(row);
});
}
function normalizeLines(text) {
const lines = text.replace(/\r/g, "").split("\n");
if (lines.length && lines[lines.length - 1] === "") {
lines.pop();
}
return lines;
}
const WORD_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
function parseXml(xmlText) {
const parser = new DOMParser();
return parser.parseFromString(xmlText, "application/xml");
}
function normalizeFieldName(raw) {
const clean = raw.replace(/\s+/g, " ").trim();
if (/MERGEFIELD/i.test(clean)) {
const parts = clean.split(/\s+/);
const idx = parts.findIndex((p) => /MERGEFIELD/i.test(p));
return parts[idx + 1] || clean;
}
return clean.replace(/\s+/g, "");
}
function getFieldType(run) {
const fldChar = run.getElementsByTagNameNS(WORD_NS, "fldChar")[0];
if (!fldChar) return null;
return (
fldChar.getAttribute("w:fldCharType") ||
fldChar.getAttributeNS(WORD_NS, "fldCharType") ||
fldChar.getAttribute("fldCharType")
);
}
function extractFieldsFromXml(xmlDoc) {
const runs = Array.from(xmlDoc.getElementsByTagNameNS(WORD_NS, "r"));
const fields = [];
let collecting = false;
let fieldName = "";
let fieldRuns = [];
let separateRun = null;
runs.forEach((run) => {
const type = getFieldType(run);
const instr = run.getElementsByTagNameNS(WORD_NS, "instrText")[0];
if (type === "begin") {
collecting = true;
fieldName = "";
fieldRuns = [run];
separateRun = null;
return;
}
if (!collecting) return;
if (instr && instr.textContent) {
fieldName += instr.textContent;
}
fieldRuns.push(run);
if (type === "separate") {
separateRun = run;
}
if (type === "end") {
const name = normalizeFieldName(fieldName);
if (name) {
fields.push({
name,
runs: fieldRuns,
separateRun,
endRun: run
});
}
collecting = false;
}
});
return fields;
}
function collectPlaceholders(zip) {
const files = zip.file(/word\/.*\.xml/);
const seen = new Set();
const ordered = [];
files.forEach((file) => {
const xml = file.asText();
const xmlDoc = parseXml(xml);
const fields = extractFieldsFromXml(xmlDoc);
fields.forEach((field) => {
if (!seen.has(field.name)) {
seen.add(field.name);
ordered.push(field.name);
}
});
});
return ordered;
}
function setFieldValue(xmlDoc, field, value) {
if (!field.separateRun || !field.endRun) return;
const parent = field.endRun.parentNode;
let node = field.separateRun.nextSibling;
while (node && node !== field.endRun) {
const next = node.nextSibling;
parent.removeChild(node);
node = next;
}
const run = xmlDoc.createElementNS(WORD_NS, "w:r");
const text = xmlDoc.createElementNS(WORD_NS, "w:t");
if (/^\s|\s$/.test(value)) {
text.setAttributeNS(
"http://www.w3.org/XML/1998/namespace",
"xml:space",
"preserve"
);
}
text.textContent = value;
run.appendChild(text);
parent.insertBefore(run, field.endRun);
}
function updateDocxFields(zip, mapping) {
const files = zip.file(/word\/.*\.xml/);
files.forEach((file) => {
const xml = file.asText();
const xmlDoc = parseXml(xml);
const fields = extractFieldsFromXml(xmlDoc);
fields.forEach((field) => {
if (Object.prototype.hasOwnProperty.call(mapping, field.name)) {
setFieldValue(xmlDoc, field, mapping[field.name] ?? "");
}
});
const serializer = new XMLSerializer();
const newXml = serializer.serializeToString(xmlDoc);
zip.file(file.name, newXml);
});
}
txtInput.addEventListener("change", async () => {
const file = txtInput.files[0];
if (!file) {
txtLines = [];
renderList(dataList, txtLines);
dataCount.textContent = "0 条";
updateButtonState();
return;
}
const text = await file.text();
txtLines = normalizeLines(text);
renderList(dataList, txtLines);
dataCount.textContent = `${txtLines.length} 条`;
updateButtonState();
});
docxInput.addEventListener("change", async () => {
const file = docxInput.files[0];
if (!file) {
placeholders = [];
docxArrayBuffer = null;
renderList(placeholderList, placeholders);
placeholderCount.textContent = "0 个";
updateButtonState();
return;
}
docxArrayBuffer = await file.arrayBuffer();
const zip = new window.PizZip(docxArrayBuffer);
placeholders = collectPlaceholders(zip);
renderList(placeholderList, placeholders);
placeholderCount.textContent = `${placeholders.length} 个`;
updateButtonState();
});
processBtn.addEventListener("click", () => {
downloadBox.style.display = "none";
if (!docxArrayBuffer) {
statusText.textContent = "未读取到 DOCX 文件。";
return;
}
if (placeholders.length === 0) {
statusText.textContent = "未识别到域占位符,请确认 DOCX 使用 Word 域(field code)。";
renderMapping({});
return;
}
const limitedPlaceholders = placeholders.slice(0, 5);
const mapping = {};
limitedPlaceholders.forEach((key, idx) => {
mapping[key] = txtLines[idx] ?? "";
});
renderMapping(mapping);
try {
const zip = new window.PizZip(docxArrayBuffer);
updateDocxFields(zip, mapping);
const out = zip.generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
const url = URL.createObjectURL(out);
downloadLink.href = url;
downloadBox.style.display = "block";
const extraLine =
placeholders.length !== 5
? `(检测到 ${placeholders.length} 个,占位符取前 5 个)`
: "";
statusText.textContent = `已写入并生成 test_update.docx ${extraLine}`;
} catch (err) {
statusText.textContent = `生成失败:${err.message || err}`;
}
});
</script>
</body>
</html>