// 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(//g, ' ')
.replace(//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);
}