| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // Read product docs (.docx) and emit trimmed plain text.
- const fs = require('fs');
- const path = require('path');
- const AdmZip = require('adm-zip');
- function docxText(p) {
- try {
- const z = new AdmZip(p);
- const x = z.readAsText('word/document.xml');
- if (!x) return '[empty]';
- return x
- .replace(/<w:tab\/>/g, ' ')
- .replace(/<w:br\/>/g, '\n')
- .replace(/<\/w:p>/g, '\n')
- .replace(/<[^>]+>/g, '')
- .replace(/\s+\n/g, '\n')
- .replace(/\n{3,}/g, '\n\n')
- .trim();
- } catch (e) {
- return '[ERR ' + e.message + ']';
- }
- }
- const targets = [
- {
- tag: 'probiotic-背景需求',
- p: 'E:\\文件资料\\产品创新2:儿童乳酸菌素片\\儿童乳酸菌素片-资料\\01 儿童装乳酸菌素片产品背景及需求0418.docx',
- },
- {
- tag: 'probiotic-说明书',
- p: 'E:\\文件资料\\产品创新2:儿童乳酸菌素片\\儿童乳酸菌素片-资料\\儿乳-乳酸菌素片说明书(0.2g).docx',
- },
- {
- tag: 'monkey-产品资料',
- p: 'E:\\文件资料\\产品创新2-江中猴菇饮产品资料.docx',
- },
- ];
- const outDir = path.resolve('data', 'product-docs');
- if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
- for (const t of targets) {
- console.log('====', t.tag, '====');
- if (!fs.existsSync(t.p)) {
- console.log('[missing]', t.p);
- continue;
- }
- const txt = docxText(t.p);
- const out = path.join(outDir, t.tag + '.txt');
- fs.writeFileSync(out, txt, 'utf-8');
- console.log('[chars]', txt.length, '-> ', out);
- }
|